Flutterにて、Firestoreに値を追加していた際、
正しいProvider
が見つからないエラーが出てしまいました。
error
1Error: Could not find the correct Provider<BookListModel> above this Consumer<BookListModel> Widget
解決策を教えて頂けると嬉しいです。
bookList
1import 'package:flutter/material.dart'; 2import 'package:flutter1/book/add/add_book.dart'; 3import 'book_list_model.dart'; 4import 'package:provider/provider.dart'; 5 6class BookList extends StatelessWidget { 7 @override 8 Widget build(BuildContext context) { 9 return ChangeNotifierProvider<BookListModel>( 10 create: (_) => BookListModel()..fetchBooks(), 11 child: Scaffold( 12 appBar: AppBar( 13 title: Text('Firebase Test'), 14 backgroundColor: Colors.purple, 15 centerTitle: true, 16 ), 17 body: Consumer<BookListModel>(builder: (context, model, child) { 18 final books = model.books; 19 final listTiles = 20 books.map((book) => ListTile(title: Text(book.title))).toList(); 21 return ListView( 22 children: listTiles, 23 ); 24 }), 25 floatingActionButton: FloatingActionButton( 26 onPressed: () async{ 27 await Navigator.push( 28 context, 29 MaterialPageRoute( 30 builder: (context) => AddBookPage(), 31 fullscreenDialog: true 32 ), 33 ); 34 }, 35 child: Icon(Icons.add), 36 shape: 37 BeveledRectangleBorder(borderRadius: BorderRadius.circular(30)), 38 backgroundColor: Colors.purple, 39 ), 40 ), 41 ); 42 } 43} 44
bookListModel
1import 'package:cloud_firestore/cloud_firestore.dart'; 2import 'package:flutter/material.dart'; 3import 'book.dart'; 4 5class BookListModel extends ChangeNotifier { 6 List<Book> books = []; 7 8 Future<void> fetchBooks() async { 9 final docs = await Firestore.instance.collection('books').getDocuments(); 10 final books = docs.documents.map((doc) => Book(doc['title'])).toList(); 11 this.books = books; 12 notifyListeners(); 13 } 14}
book
1class Book { 2 String title; 3 4 Book(this.title); 5}
エラー全文です????
error
1Error: Could not find the correct Provider<BookListModel> above this Consumer<BookListModel> Widget 2 3This likely happens because you used a `BuildContext` that does not include the provider 4of your choice. There are a few common scenarios: 5 6- The provider you are trying to read is in a different route. 7 8 Providers are "scoped". So if you insert of provider inside a route, then 9 other routes will not be able to access that provider. 10 11- You used a `BuildContext` that is an ancestor of the provider you are trying to read.
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。