找不到提供者-Flutter

时间:2019-12-20 08:36:03

标签: flutter stream provider

我想向DecksList小部件提供流,所以我在MyApp中初始化了提供程序。它引发ProviderNotFoundError。发生这种行为的原因可能是什么?

I/flutter (27768): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter (27768): The following ProviderNotFoundError was thrown building NotificationListener<KeepAliveNotification>:
I/flutter (27768): Error: Could not find the correct Provider<Stream<List<Deck>>> above this DecksList Widget

打开图像以查看MyApp小部件: app widget

Home小部件内的DecksList:

class DecksList extends StatefulWidget {
  @override
  _DecksListState createState() => _DecksListState();
}

class _DecksListState extends State<DecksList> {
  Stream<List<Deck>> decks;

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    decks = Provider.of<Stream<List<Deck>>>(context);
  }

  @override
  Widget build(BuildContext context) {
    return StreamBuilder<List<Deck>>(
        stream: decks,
        builder: (context, snapshot) {
          final decksList = snapshot.data ?? List();
          return ListView.builder(
//          to scroll lists together
            physics: ClampingScrollPhysics(),
            shrinkWrap: true,
            itemCount: decksList.length,
            itemBuilder: (BuildContext context, int index) {
              final Deck itemDeck = decksList[index];
              return DeckTile(
                deckItem: itemDeck,
                onPressed: () {
                  Navigator.push(context,
                      CupertinoPageRoute(builder: (BuildContext context) {
                    return FlashcardsDeck(
                      deckId: itemDeck.id,
                    );
                  }));
                },
                onLongPress: () {},
              );
            },
          );
        });
  }
}

1 个答案:

答案 0 :(得分:0)

问题出在停泊的图书馆。 这是一个问题https://github.com/simolus3/moor/issues/329