我正在尝试使用“ OnGenerateRoute”,但出现了错误,但是当我直接使用“ Routes”时,它运行良好“在构建CatMealsScreen(脏,依赖项:[_ ModalScopeStatus])时引发了以下NoSuchMethodError: 方法'[]'在null上被调用。 接收者:null 尝试调用:(“ tittle”)“
import 'dart:io';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:resturant/widgets/meal.dart';
import '../DUMMY_CATEGORIES.dart';
import 'package:resturant/widgets/mealItem.dart';
class CatMealsScreen extends StatefulWidget {
static const routesNames = '/CatMealScreen';
final List<Meal> _availableMeals2;
CatMealsScreen(this._availableMeals2);
@override
_CatMealsScreenState createState() => _CatMealsScreenState();
}
class _CatMealsScreenState extends State<CatMealsScreen> {
String catTittle;
List<Meal> displayMeals;
bool _loadInitData = false;
@override
void didChangeDependencies() {
// TODO: implement didChangeDependencies
if (_loadInitData == false) {
final routeArg =
ModalRoute.of(context).settings.arguments as Map<String, String>;
catTittle = routeArg['tittle'];
final catId = routeArg['id'];
displayMeals = widget._availableMeals2.where((meal) {
return meal.categories.contains(catId);
}).toList();
_loadInitData = true;
}
super.didChangeDependencies();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(catTittle),
),
body: ListView.builder(
itemBuilder: (ctx, index) {
return MealItem(
id: displayMeals[index].id,
title: displayMeals[index].title,
imageUrl: displayMeals[index].imageUrl,
duration: displayMeals[index].duration,
affordability: displayMeals[index].affordability,
complexity: displayMeals[index].complexity,
);
},
itemCount: displayMeals.length));
}
}
主文件
import 'dart:io';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:resturant/DUMMY_CATEGORIES.dart';
import 'package:resturant/screenWidget/mealDetails.dart';
import 'package:resturant/screenWidget/settings.dart';
import 'package:resturant/screenWidget/tabsScreen.dart';
import 'package:resturant/widgets/meal.dart';
import 'screenWidget/catMealsScreen.dart';
import 'screenWidget/catScreen.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
Map<String, bool> _filters = {
'gluten': false,
'lactose': false,
'vegan': false,
'vegetarian': false,
};
List<Meal> _availableMeals = DUMMY_MEALS;
List<Meal> _favouriteMeals = [];
void _setFilter(Map<String, bool> filterData) {
setState(() {
_filters =
filterData; // the new value of filters will equal to the value of filterData
// that when the setstate() work it will work on the new value of filter
_availableMeals = DUMMY_MEALS.where((meal) {
if (_filters['gluten'] == true && !meal.isGlutenFree) {
return false;
}
if (_filters['lactose'] == true && !meal.isLactoseFree) {
return false;
}
if (_filters['vegan'] == true && !meal.isVegan) {
return false;
}
if (_filters['vegetarian'] == true && !meal.isVegetarian) {
return false;
}
return true;
}).toList();
});
}
void _toggleMeals(String mealId) {
final existingMeal =
_favouriteMeals.indexWhere((element) => element.id == mealId);
if (existingMeal >= 0) {
setState(() {
_favouriteMeals.removeAt(existingMeal);
});
} else {
setState(() {
_favouriteMeals.add(
DUMMY_MEALS.firstWhere((element) => element.id == mealId),
);
});
}
}
bool isFavMeal(String id) {
return _favouriteMeals.any((element) => element.id == id);
}
@override
Widget build(BuildContext context) {
return Platform.isIOS
? CupertinoApp()
: MaterialApp(
title: 'DeliMeals',
theme: ThemeData(
primarySwatch: Colors.pink,
accentColor: Colors.amber,
canvasColor: Color.fromRGBO(255, 255, 204, 1),
fontFamily: 'Raleway',
textTheme: ThemeData.light().textTheme.copyWith(
body1: TextStyle(
color: Color.fromRGBO(20, 51, 51, 1),
),
body2: TextStyle(
color: Color.fromRGBO(20, 51, 51, 1),
),
title: TextStyle(
fontFamily: 'RobotoCondensed',
fontSize: 18,
fontWeight: FontWeight.bold),
),
),
home: TabsScreen(_favouriteMeals),
routes: {
CatMealsScreen.routesNames: (ctx) =>
CatMealsScreen(_availableMeals),
MealDetail.routeNmaes: (ctx) => MealDetail(_toggleMeals , isFavMeal),
Settings.routeNames: (ctx) => Settings(_setFilter, _filters),
},
onGenerateRoute: (settings) {
print(settings.arguments);
return MaterialPageRoute(builder: (ctx) => CatMealsScreen());
},
onUnknownRoute: (setting) {
return MaterialPageRoute(builder: (ctx) => CatMealsScreen());
// both produce the same error
},
);
}
}
答案 0 :(得分:0)
我想您需要对在switch
中获得的设置进行onGenerateRoute
声明,因此根据获得的名称(基本上是您提供的路线名称),您应该返回正确的屏幕您想要导航到。我还认为您不需要routes
属性,因为您正在使用onGenerateRoute
。
onGenerateRoute: (settings) {
switch(settings.name){
case CatMealsScreen.routesNames:
return CatMealsScreen();
}
},