【问题标题】:flutter provider MultiProvider using StreamProvider使用 StreamProvider 的颤振提供者 MultiProvider
【发布时间】:2019-12-08 11:17:27
【问题描述】:

我正在使用 Provider (provider: 3.0.0+1) 开发一个 Flutter 应用。我正在使用带有控制器的 StreamProvider 的 MultiProvider。但我总是遇到错误。

下面是我的代码

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

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();
 }
}

但我总是收到此错误:

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)

******更新******

将 Multiprovider 更改为以下代码

 providers: [
    StreamProvider<List<Recipe>>.value(value: RecipeStreamService().controllerOut)
  ],

在我使用它的 HomePage.dart 中,我有

final recipeService = Provider.of<List<Recipe>>(context);

现在,recipeService 总是以 null 的形式出现

谢谢

【问题讨论】:

  • controllerOut 好像不是流
  • 嗨 Remi,但它是在 RecipeStreamService 中定义的,获取 controllerOut => _controller.stream;
  • 抱歉,我看错了错误。您有一个列表流,并且您正在尝试将其分配给 RecipeStreamService 的流
  • 嗨 Remi,所以它应该是 StreamProvider>.value(value: RecipeStreamService().controllerOut)
  • 你可以试试controllerOut.add 代替controllerIn.add 吗? dart.dev/articles/libraries/creating-streams

标签: flutter dart stream provider flutter-provider


【解决方案1】:

您看到错误的原因是因为您传递给StreamProvider&lt;T&gt;类型参数T 不匹配返回给create.

在这种情况下,您希望流式传输 List&lt;Recipe&gt; 类型的值。
因此,您还应该将其传递给泛型类型:

StreamProvider<List<Recipe>>.value(value: stream)

stream 是您的List&lt;Recipe&gt; 流。


如果Provider.of&lt;List&lt;Recipe&gt;&gt;(context) 为您返回null,则表示您尚未向流中添加值。

如果您想在流发出值之前拥有null 以外的其他内容,您可以传递initialValue

StreamProvider<List<Recipe>>.value(
  value: stream,
  initialValue: initialRecipes,
)

【讨论】:

    猜你喜欢
    • 2021-11-01
    • 2020-05-10
    • 2020-12-24
    • 2022-08-19
    • 1970-01-01
    • 2022-01-15
    • 2020-06-04
    • 2021-06-18
    • 1970-01-01
    相关资源
    最近更新 更多