我正在学习在Flutter应用中使用与Firestore数据库通信的提供程序。
最初,该应用程序从Firestore集合的Listview小部件的ProductPage中加载了我的文档,然后在点击ListTile时可以编辑或删除该文档。另外,应用栏上有一个按钮,用户可以从中添加产品。很好。
然后,我向该应用添加了身份验证流程,该流程目前有效。但是要听整个应用程序中的authChanges,我必须从main.dart页面更改初始提供程序的工作方式。
我从
更改了代码class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
final firestoreService = FirestoreService();
return MultiProvider(
providers: [
ChangeNotifierProvider( create: (context)=> ProductController(),),
StreamProvider(create: (context) => firestoreService.fetchAllProducts())
],
child: MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: Authenticate(),
),
);
}
}
并将其更改为: Main.dart代码:
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return StreamProvider<User>.value(
value: AuthService().user,
child: MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: Wrapper(),
),
);
}
}
我设法将所有内容连接在一起,以使用户在身份验证后即可访问产品。
正在按预期检索产品,但是,现在尝试导航到editPage
Navigator.of(context).push(
CupertinoPageRoute(
builder: (context) => EditProduct(),
),
或转到新产品页面
ListView.builder(
itemCount: products.length,
itemBuilder: (context, index) {
return ListTile(
title: Text(products[index].productName),
trailing: Text(products[index].price.toString()),
onTap: () {
**Navigator.of(context).push(
MaterialPageRoute(
builder: (BuildContext context) => EditProduct(products[index])**,
),
);
},
);
})
,引发错误
Error: Could not find the correct Provider<ProductController> above this EditProduct Widget
Wrapper.dart代码:
class Wrapper extends StatelessWidget {
@override
Widget build(BuildContext context) {
final firestoreService = FirestoreService();
final user = Provider.of<User>(context);
//return either Products Page or Authentication Page
if (user == null) {
return Authenticate();
}
else {
// return StreamProvider(
// create: (context) => firestoreService.fetchAllProducts(),
// child: Products(),
// );
return MultiProvider(
providers: [
ChangeNotifierProvider<ProductController>( create: (context)=> ProductController(),),
StreamProvider(create: (context) => firestoreService.fetchAllProducts())
],
child: Consumer<ProductController>(builder: (context, provider, child) =>Products()) ,
);
}
}
}
产品页面:
@override
Widget build(BuildContext context) {
final products = Provider.of<List<Product>>(context);
return Scaffold(
appBar: AppBar(
...button that leads to editPage..
Navigator.of(context).push(
CupertinoPageRoute(
builder: (context) => EditProduct(),
),
);
},
),
],
),
body:
ListView.builder(
itemCount: products.length,
itemBuilder: (context, index) {
return ListTile(
title: Text(products[index].productName),
trailing: Text(products[index].price.toString()),
onTap: () {
Navigator.of(context).push(
MaterialPageRoute(
builder: (BuildContext context) => EditProduct(products[index]),
),
);
},
);
})
);
}
}
“编辑/添加产品页面”构建器
Widget build(BuildContext context) {
final productController = Provider.of<ProductController>(context);
return Scaffold(.....more widgets....);