前提・実現したいこと
DocumentSnapshotのdocumentで、Firebaseにあるフィールドの値を取得したい。
背景:
flutterの初学者で、練習の為にTODOアプリをFirebaseを用いて作成しています。
・追加したタスクをリストに表示(StreamBuilderを使って)
・タスクを横スワイプで削除
は実装でき、タスクを編集する段階となっています。
やりたいこと:
下記のコードの7行目と8行目の★部分、
String task
String subtask
の変数にFirebaseのデータベースにある、
collection['todo']というコレクションの中の、
document['task']というフィールドの値と、
document['subtask']の値を代入したいです。
該当のソースコード
flutter
1import 'dart:html'; 2import 'package:cloud_firestore/cloud_firestore.dart'; 3import 'package:flutter/material.dart'; 4 5class UapdatePage extends StatelessWidget { 6 UapdatePage({this.document}); 7 final DocumentSnapshot document; 8 9 String task = ''; //★ここにdocument['task']の値を代入したい 10 String subtask = ''; //★ここにdocument['subtask']の値を代入したい 11 12 @override 13 Widget build(BuildContext context) { 14 return Scaffold( 15 appBar: AppBar( 16 title: Text('Update_TODO'), 17 ), 18 body: Column( 19 children: <Widget>[ 20 Padding( 21 padding: const EdgeInsets.all(20.0), 22 child: Text('Taskを編集'), 23 ), 24 TextField( 25 controller: TextEditingController(text: document['task']), 26 decoration: InputDecoration( 27 border: OutlineInputBorder(), hintText: ' Taskを追加'), 28 onChanged: (text) { 29 task = text; 30 }, 31 ), 32 Padding( 33 padding: const EdgeInsets.all(20.0), 34 child: Text('詳細を編集'), 35 ), 36 TextField( 37 controller: TextEditingController(text: document['subtask']), 38 decoration: InputDecoration( 39 border: OutlineInputBorder(), hintText: '詳細を記載'), 40 onChanged: (text) { 41 subtask = text; 42 }, 43 ), 44 ElevatedButton.icon( 45 icon: const Icon( 46 Icons.system_update_alt_rounded, 47 color: Colors.white, 48 ), 49 label: const Text('update'), 50 onPressed: () async { 51 try { 52 await updateforFirebase(document.id); 53 await openShowDialog(context, task); 54 Navigator.pop(context); 55 } catch (e) { 56 await openShowDialogError(context, e.toString()); 57 } 58 }, 59 ), 60 ], 61 ), 62 ); 63 } 64 65 //ここから画面関係ない 66 Future updateforFirebase(String id) { 67 if (task.isEmpty) { 68 throw ('taskを更新してください'); 69 } 70 FirebaseFirestore.instance.collection('todo').doc(id).update({ 71 'task': task, 72 'subtask': subtask, 73 }); 74 } 75 76 Future openShowDialog(BuildContext context, String message) { 77 return showDialog( 78 context: context, 79 builder: (_) { 80 return AlertDialog( 81 title: Text("更新完了"), 82 content: Text("『${message}』に更新しました。"), 83 actions: <Widget>[ 84 TextButton( 85 child: Text("OK"), 86 onPressed: () => Navigator.pop(context), 87 ), 88 ], 89 ); 90 }, 91 ); 92 } 93 94 Future openShowDialogError(BuildContext context, String message) { 95 return showDialog( 96 context: context, 97 builder: (_) { 98 return AlertDialog( 99 title: Text(message), 100 actions: <Widget>[ 101 TextButton( 102 child: Text("OK"), 103 onPressed: () => Navigator.pop(context), 104 ), 105 ], 106 ); 107 }, 108 ); 109 } 110} 111 112ソースコード
試したこと
・document.get(task)で引っ張って来れないか
などなど初学者ながら調べて色々試したのですが分かりませんでした。
補足情報(FW/ツールのバージョンなど)
AndoroidStadioにてコードを書いています。
Android Studio 4.1.3 Build #AI-201.8743.12.41.7199119, built on March 11, 2021 Runtime version: 1.8.0_242-release-1644-b3-6915495 x86_64 VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o macOS 10.16 GC: ParNew, ConcurrentMarkSweep Memory: 1237M Cores: 8 Registry: ide.new.welcome.screen.force=true, external.system.auto.import.disabled=true Non-Bundled Plugins: Dart, io.flutter
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/06/06 03:00 編集