當我嘗試將從路由中獲得的引數與串列的值進行比較時,我遇到了上述錯誤。
MealDetailScreen 小部件
import 'package:flutter/material.dart';
import '../dummy_data.dart';
class MealDetailScreen extends StatelessWidget {
static const detailScreenRouteName = '/meal-detail';
@override
Widget build(BuildContext context) {
final mealId = ModalRoute.of(context)!.settings.arguments;
final selectedMeal = DUMMY_MEALS.firstWhere((meal) => meal.id == mealId);
return Scaffold(
appBar: AppBar(title: Text('$mealId')),
body: Container(
child: Column(
children: [
Container(
height: 300,
width: double.infinity,
child: Image.network(
selectedMeal.imageUrl,
fit: BoxFit.cover,
),
)
],
),
),
);
}
}
如果我嘗試將可選引數 'orElse' 添加到 firstWhere 函式,我仍然會收到錯誤:這次是回傳型別 'Null' 不是 'Meal',這是閉包背景關系所要求的。
這是我用來比較 id 的串列。
const DUMMY_MEALS = [
Meal(
isVegetarian: false,
isLactoseFree: false,
isVegan: false,
id: 'm1',
categories: [
'c1',
'c2',
],
title: 'Spaghetti with Tomato Sauce',
affordability: Affordability.Affordable,
complexity: Complexity.Simple,
imageUrl: '...',
duration: 20,
ingredients: ['4 Tomatoes', '...'],
steps: ['Cut the tomatoes and the onion into small pieces.', '...'],
isGlutenFree: false,
),
];
這就是我將 id 作為引數傳遞的方式
void selectMeal(BuildContext context) {
Navigator.of(context)
.pushNamed(MealDetailScreen.detailScreenRouteName, arguments: {id});
}
餐型
import 'package:flutter/foundation.dart';
enum Complexity { Simple, Challenging, Hard }
enum Affordability { Affordable, Pricey, Luxurious }
class Meal {
final String id;
final List<String> categories;
final String title;
final String imageUrl;
final List<String> ingredients;
final List<String> steps;
final int duration;
final Complexity complexity;
final Affordability affordability;
final bool isGlutenFree;
final bool isLactoseFree;
final bool isVegan;
final bool isVegetarian;
const Meal(
{required this.id,
required this.categories,
required this.title,
required this.imageUrl,
required this.ingredients,
required this.steps,
required this.duration,
required this.complexity,
required this.affordability,
required this.isGlutenFree,
required this.isLactoseFree,
required this.isVegan,
required this.isVegetarian});
}
uj5u.com熱心網友回復:
路過時arguments: {id}你正在路過_HashSet<String>。但是對于單值id字串就足夠了,否則使用地圖。
在這種情況下,傳遞引數將是
Navigator.of(context)
.pushNamed(MealDetailScreen.detailScreenRouteName, arguments: id);
在迭代DUMMY_MEALS串列時,我們可能會得到一個id未包含在DUMMY_MEALS串列中的串列。在這種情況下,您可以在orElse狀態上創建并傳遞 emptyMeal或僅用于try catch處理例外。
Meal? selectedMeal;
try {
final selectedMeal = DUMMY_MEALS.firstWhere((meal) => meal.id == mealId);
} catch (e) {
print(e.toString());
}
雖然selectedMeal可以為空,但我們可以檢查它是否包含膳食。
return Scaffold(
appBar: AppBar(title: Text('$mealId')),
body: selectedMeal == null
? Text("Cound not find data")
: Container(
child: Column(
children: [
Text(selectedMeal.title),
],
),
),
);
uj5u.com熱心網友回復:
答案已經給出。但我想提一下,如果您使用 pushNamed 方法,建議您使用 onGenerateRoute 管理傳遞引數。所以你不需要空檢查引數或背景關系。
ModalRoute.of(context)?.settings?.arguments?
MaterialApp(
onGenerateRoute: (settings) {
if (settings.name == PassArgumentsScreen.routeName) {
final args = settings.arguments as ScreenArguments;
return MaterialPageRoute(
builder: (context) {
return PassArgumentsScreen(
title: args.title,
message: args.message,
);
},
);
}
assert(false, 'Need to implement ${settings.name}');
return null;
},
)
參考:將引數傳遞給命名路由
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/351460.html
