【发布时间】:2020-11-18 03:42:37
【问题描述】:
我有一个使用颤振提供程序的列表。但是 firestore 数据有效。问题是那个提供程序。错误是在 null 上调用了 getter 长度。我在stackoverflow中搜索了这个问题,我尝试了每一个答案,但他们没有解决我的问题。下面是包含的代码:
游戏服务类
class GamesServices {
String collection = 'games';
Firestore _firestore = Firestore.instance;
Future<List<GamesModel>> getGames() async {
_firestore.collection(collection).getDocuments().then((result) {
List<GamesModel> gameList = <GamesModel>[];
for (DocumentSnapshot game in result.documents) {
gameList.add(GamesModel.fromSnapshot(game));
}
return gameList;
});
}
提供者类
class BoshphelmProvider with ChangeNotifier {
GamesServices _gamesServices = GamesServices();
List<GamesModel> _games = <GamesModel>[];
List<GamesModel> get games => _games;
BoshphelmProvider.initialize() {
loadGames();
}
loadGames() async {
_games = await _gamesServices.getGames();
notifyListeners();
}
}
主类
void main() {
setupLocator();
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
return ChangeNotifierProvider.value(
value: BoshphelmProvider.initialize(),
child: MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Boshphelm',
theme: ThemeData(
primaryColor: Constant.primaryColor,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: HomePage(),
),
);
}
}
【问题讨论】:
标签: flutter flutter-layout flutter-provider