検索でDatabase内の言葉を検索対象にしたい
Flutter/Dartで辞書アプリを作っています。
こちらのソースコード(https://blog.usejournal.com/flutter-search-in-listview-1ffa40956685)を元に検索機能を追加していたのですが、queryを元にDatabaseから検索結果を割り出す作業がうまくいきません。
問題のソースコード
Dart
1import 'package:flutter/material.dart'; 2import 'package:provider/provider.dart'; 3 4import 'package:dictionary/Services/database.dart'; 5import 'package:dictionary/Models/verbs_data.dart'; 6 7import 'words_list.dart'; 8import 'package:dictionary/MainTabs/Search/search_appbar_button.dart'; 9 10class Search extends StatefulWidget { 11 Search({Key key}) : super(key: key); 12 13 14 _Search createState() => new _Search(); 15} 16 17class _Search extends State<Search> { 18 TextEditingController editingController = TextEditingController(); 19 20 final duplicateItems = List<VerbsData>(); 21 final items = List<VerbsData>(); 22 final VerbsData verbsData; 23 24 _Search({this.verbsData}); 25 26 27 void initState() { 28 items.addAll(duplicateItems); 29 super.initState(); 30 } 31 32 void filterSearchResults(String query) { 33 List<VerbsData> dummySearchList = List<VerbsData>(); 34 dummySearchList.addAll(duplicateItems); 35 if (query.isNotEmpty) { 36 List<VerbsData> dummyListData = List<VerbsData>(); 37 dummySearchList.forEach((item) { 38 if (verbsData.romaji.contains(query)) { 39 dummyListData.add(item); 40 } 41 }); 42 setState(() { 43 items.clear(); 44 items.addAll(dummyListData); 45 }); 46 return; 47 } else { 48 setState(() { 49 items.clear(); 50 items.addAll(duplicateItems); 51 }); 52 } 53 } 54 55 56 Widget build(BuildContext context) { 57 return StreamProvider<List<VerbsData>>.value( 58 value: DatabaseService().verbs, 59 child: Scaffold( 60 appBar: AppBar( 61 leading: ButtonOnAppBar(), 62 title: Padding( 63 padding: const EdgeInsets.symmetric(vertical: 4.0), 64 child: TextField( 65 onChanged: (value) { 66 filterSearchResults(value); 67 }, 68 controller: editingController, 69 decoration: InputDecoration( 70 labelText: "Search", 71 fillColor: Colors.white, 72 filled: true, 73 prefixIcon: Icon(Icons.search), 74 border: OutlineInputBorder( 75 borderRadius: BorderRadius.all(Radius.circular(25.0)))), 76 ), 77 ), 78 ), 79 body: SingleChildScrollView( 80 child: Column( 81 crossAxisAlignment: CrossAxisAlignment.start, 82 children: <Widget>[DropdownForSorts(), WordsList()], 83 ), 84 ), 85 ), 86 ); 87 } 88}
関連ソースコード
verbs_data.dart
Dart
1class VerbsData { 2 final String main_word; 3 final int difficulty; 4 final int frequency; 5 final String en_meaning; 6 final String romaji; 7 final String furigana; 8 final String infinitive; 9 10 VerbsData( 11 {this.main_word, 12 this.difficulty, 13 this.frequency, 14 this.en_meaning, 15 this.romaji, 16 this.furigana, 17 this.infinitive,}); 18}
database.dart
Dart
1import 'package:cloud_firestore/cloud_firestore.dart'; 2import 'package:dictionary/Models/verbs_data.dart'; 3 4class DatabaseService { 5 final CollectionReference verbCollection = 6 Firestore.instance.collection('verbs'); 7 8 9 List<VerbsData> _verbsDataFromSnapshot(QuerySnapshot snapshot) { 10 return snapshot.documents.map((doc) { 11 return VerbsData( 12 main_word: doc.data['main_word'] ?? 'xx', 13 difficulty: doc.data['difficulty'] ?? 0, 14 frequency: doc.data['frequency'] ?? 0, 15 en_meaning: doc.data['en_meaning'] ?? 'xx', 16 romaji: doc.data['romaji'] ?? 'xx', 17 furigana: doc.data['furigana'] ?? 'xx', 18 infinitive: doc.data['infinitive'] ?? 'xx', 19 ); 20 }).toList(); 21 }
words_list.dart
Dart
1import 'package:dictionary/Models/singleton.dart'; 2import 'package:dictionary/Models/verbs_data.dart'; 3 4import 'package:provider/provider.dart'; 5import 'package:flutter/material.dart'; 6import 'package:dictionary/MainTabs/Search/Words/main_word_page.dart'; 7 8class WordsList extends StatefulWidget { 9 10 _WordsListState createState() => _WordsListState(); 11} 12 13class _WordsListState extends State<WordsList> { 14 15 Widget build(BuildContext context) { 16 final verbs = Provider.of<List<VerbsData>>(context); 17 18 return ListView.builder( 19 shrinkWrap: true, 20 itemCount: verbs?.length ?? 0, 21 itemBuilder: (context, index) { 22 return WordsTile(verbsData: verbs[index]); 23 }, 24 ); 25 } 26} 27 28class WordsTile extends StatelessWidget { 29 final VerbsData verbsData; 30 31 WordsTile({this.verbsData}); 32 33 34 Widget build(BuildContext context) { 35 return ListTile( 36 title: RichText( 37 text: TextSpan( 38 style: DefaultTextStyle.of(context).style, 39 children: <TextSpan>[ 40 TextSpan( 41 text: ('${verbsData.main_word}'), 42 style: TextStyle(fontWeight: FontWeight.bold, fontSize: 20.0), 43 ), 44 TextSpan( 45 text: (' ${verbsData.romaji}'), 46 style: TextStyle( 47 fontWeight: FontWeight.w400, 48 fontSize: 20.0, 49 fontStyle: FontStyle.italic)), 50 TextSpan( 51 text: (' ${verbsData.en_meaning}'), 52 style: TextStyle(fontWeight: FontWeight.w400, fontSize: 20.0)) 53 ]), 54 ), 55 onTap: () { 56 Verb verb = Verb.getInstance(); 57 verb.verb = verbsData; 58 59 Navigator.push( 60 context, 61 MaterialPageRoute( 62 builder: (context) => MainWordPage(), 63 ), 64 ); 65 }, 66 ); 67 } 68} 69
試したこと
「問題のソースコード」にある
final duplicateItems = List<VerbsData>(); final items = List<VerbsData>(); final VerbsData verbsData;
を、words_list.dartのようにProviderを使用してみましたが(下記)
final duplicateItems = Provider.of<List<VerbsData>>(context); final items = Provider.of<List<VerbsData>>(context); final VerbsData verbsData;
この変数宣言がBuildContext外にあるので、(context) の部分で "static members can be accessed in initializers" とエラーが出ます。そもそもstatic memberとは具体的にどのようなものなのか、よくわかりません。
補足情報
Android Studioにて開発中。
プログラミング自体初学者ですので、矛盾等あるかもしれません。細かいところまでご指摘いただけるとありがたいです。よろしくお願いします。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/07/18 15:32
2020/07/20 00:52
2020/07/22 03:38