簡體   English   中英

使用 StreamProvider 的顫振提供者 MultiProvider

[英]flutter provider MultiProvider using StreamProvider

我正在使用 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)

行:main.dart 中的 20:80 是 (RecipeStreamService().controllerOut)

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

將 Multiprovider 更改為以下代碼

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

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

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

現在, recipeService 總是為空

謝謝

您看到錯誤的原因是您作為T傳遞給StreamProvider<T>類型參數與您返回的內容不匹配create

在這種情況下,您希望流傳輸List<Recipe>類型的值
因此,您還應該為泛型類型傳遞它:

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

其中stream是您的List<Recipe>流。


如果Provider.of<List<Recipe>>(context)為您返回null ,則表示您尚未向流添加值。

如果您想在流發出值之前擁有null以外的其他內容,則可以傳遞initialValue

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

根據此問題的答案StreamProvider不更新列表

RecipeStreamService()將啟動一個新實例,

但是我仍然不能使streamprovider正常工作

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM