我正在使用Provider
软件包来构建Flutter应用,以进行应用的状态管理。
这是我通过祖先小部件传递提供者的方式
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Ecommerce App',
theme: ThemeData(
visualDensity: VisualDensity.adaptivePlatformDensity,
primaryColor: eDarkPrimaryColor,
accentColor: eAccentColor,
backgroundColor: eDefaultPrimaryColor,
dividerColor: eDivideColor,
scaffoldBackgroundColor: eLightPrimaryColor,
textTheme:
Theme.of(context).textTheme.apply(displayColor: ePrimaryTextColor),
),
themeMode: ThemeMode.system,
home: Provider<DatabaseRepo>(
create: (context) => Database(),
child: HomePageBase(
database: Database(),
),
),
);
}
这里是DatabaseRepo
类。
abstract class DatabaseRepo {
Stream<List<CategoryModel>> streamCategory();
Stream<List<SubCategoryModel>> streamSubCategories(String categoryId);
}
class Database implements DatabaseRepo {
@override
Stream<List<CategoryModel>> streamCategory() =>
FirebaseService.instance.collectionStream(
path: APIPath.productsCategory(),
builder: (data, id) => CategoryModel.fromMap(data, id),
);
@override
Stream<List<SubCategoryModel>> streamSubCategories(String categoryId) =>
FirebaseService.instance.collectionSubCategories(
path: APIPath.productsSubCategory(categoryId),
builder: (data) => SubCategoryModel.fromMap(data),
);
}
这里是我用streamCategory()
类调用的FirebaseService函数
Stream<List<T>> collectionStream<T>({
@required String path,
@required T builder(Map<String, dynamic> data, String documentId),
}) {
final ref = Firestore.instance.collection(path).limit(6);
final snapshots = ref.snapshots();
snapshots.listen((event) => print('snapshsot ${event.documents}'));
return snapshots.map(
(snapshots) => snapshots.documents
.map(
(e) => builder(e.data, e.documentID),
)
.toList(),
);
}
我真的不知道到底发生了什么。这是错误的详细信息:
这是我正在使用的依赖项。
cupertino_icons: ^0.1.3
font_awesome_flutter: ^8.8.1
cloud_firestore: ^0.13.5
provider: ^4.1.3
carousel_slider: ^2.2.1
sticky_headers: ^0.1.8