[英]flutter provider MultiProvider using StreamProvider
I am developing a Flutter app using Provider (provider: 3.0.0+1).我正在使用 Provider(提供程序:3.0.0+1)开发 Flutter 应用程序。 I am using MultiProvider using StreamProvider with controller.
我正在使用带有控制器的 StreamProvider 的 MultiProvider。 But I am always getting an error.
但我总是收到错误。
Below is my code下面是我的代码
main.dart main.dart
Widget build(BuildContext context) {
return MultiProvider(
providers: [
StreamProvider<RecipeStreamService>.value(value: RecipeStreamService().controllerOut)
],
child: MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Home Food',
routes: {
'/register': (BuildContext context) => RegisterPage(),
'/login': (BuildContext context) => LoginPage()
},
theme: ThemeData(
primaryColor: Colors.lightBlue[300],
accentColor: Colors.green[300],
textTheme: TextTheme(
headline: TextStyle(fontSize: 42.0, fontWeight: FontWeight.bold, color: Colors.black54),
title: TextStyle(fontSize: 22.0, fontStyle: FontStyle.italic),
body1: TextStyle(fontSize: 18.0),
button: TextStyle(fontSize: 20.0,fontWeight: FontWeight.normal, color: Colors.white)
)
),
home: HomePage(title: 'Home'),
),
);
}
RecipeStreamService.dart RecipeStreamService.dart
class RecipeStreamService {
List<Recipe> _recipes = <Recipe>[];
final _controller = StreamController<List<Recipe>>.broadcast();
get controllerOut => _controller.stream;
get controllerIn => _controller.sink;
RecipeStreamService() {
getRecipes();
}
addNewRecipe(Recipe recipe) {
_recipes.add(recipe);
controllerIn.add(_recipes);
}
getRecipes() async{
List<Map<String, dynamic>> result = await ApiService().getRecipes();
List<Recipe> data = result.map((data) => Recipe.fromMap(data)).toList();
data.map((f) => addNewRecipe(f));
}
void dispose() {
_controller.close();
}
}
But I Am always getting this error:但我总是收到此错误:
type '_BroadcastStream<List<Recipe>>' is not a subtype of type 'Stream<RecipeStreamService>'
I/flutter (16880): When the exception was thrown, this was the stack:
I/flutter (16880): #0 MyApp.build (package:app_recipe/main.dart:20:80)
Line: 20:80 in main.dart is (RecipeStreamService().controllerOut)行:main.dart 中的 20:80 是 (RecipeStreamService().controllerOut)
****** UPDATE ****** ****** 更新 ******
Changed Multiprovider to below code将 Multiprovider 更改为以下代码
providers: [
StreamProvider<List<Recipe>>.value(value: RecipeStreamService().controllerOut)
],
Also in HomePage.dart, where I use it, I have同样在我使用它的 HomePage.dart 中,我有
final recipeService = Provider.of<List<Recipe>>(context);
Now, recipeService is always coming as null现在, recipeService 总是为空
Thanks谢谢
The reason you are seeing your error is because the type parameter you are passing to StreamProvider<T>
as T
is not matching what you are returning to create
.您看到错误的原因是您作为
T
传递给StreamProvider<T>
的类型参数与您返回的内容不匹配create
。
In this case, you want to stream values of type List<Recipe>
.在这种情况下,您希望流式传输
List<Recipe>
类型的值。
Therefore, you should also pass that for the generic type:因此,您还应该为泛型类型传递它:
StreamProvider<List<Recipe>>.value(value: stream)
Where the stream
is your List<Recipe>
stream.其中
stream
是您的List<Recipe>
流。
If Provider.of<List<Recipe>>(context)
is returning null
for you, it means that you have not added a value to the stream.如果
Provider.of<List<Recipe>>(context)
为您返回null
,则表示您尚未向流添加值。
If you want to have something other than null
before you stream emits a value, you can pass initialValue
:如果您想在流发出值之前拥有
null
以外的其他内容,则可以传递initialValue
:
StreamProvider<List<Recipe>>.value(
value: stream,
initialValue: initialRecipes,
)
according to the answer of this question StreamProvider not updating List 根据此问题的答案StreamProvider不更新列表
RecipeStreamService() will start a new instance, RecipeStreamService()将启动一个新实例,
but i still can't make the streamprovider work well 但是我仍然不能使streamprovider正常工作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.