Beginning FlutterFire

Aug 30 2022 · Dart 2.16, Flutter 3.0, Visual Studio Code 1.69

Part 3: Read & Write Data with the Cloud Firestore

14. Add & Update Data into the Firestore Database

Episode complete

Play next episode

Next
About this episode
Leave a rating/review
See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 13. Create the Activity Detail Screen Next episode: 15. Delete Data from the ListView

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

In the Activity Detail screen, we need to add the code that saves an Activity into the Firestore database.

Future saveUser() async { 
  widget.activity.description = txtDescription.text; 
  widget.activity.day = txtDay.text; 
  widget.activity.beginTime = txtBeginTime.text; 
  widget.activity.endTime = txtEndTime.text; 
  if (galleryImage != null) { 
    widget.activity.imagePath = galleryImage?.path ?? ''; 
  } 
  final result = await helper.insertActivity(widget.activity); 
  return result; 
} 
onPressed: () { 
 saveUser(); 
 final route = MaterialPageRoute<dynamic>( 
     builder: (context) => const ActivitiesScreen()); 
 final Future push = Navigator.push(context, route); 
}, 
child: const Icon(Icons.add), 
          onPressed: () { 
            final activity = Activity('', '', '', '', '', ''); 
            final route = MaterialPageRoute<dynamic>( 
                builder: (context) => ActivityDetailScreen(activity)); 
            final Future push = Navigator.push(context, route); 
          }), 
onTap: () { 
  final route = MaterialPageRoute<dynamic>( 
      builder: (context) => ActivityScreen(activity)); 
  final Future push = Navigator.push(context, route); 
}, 
Future updateActivity(Activity activity, String id) async { 
    try { 
      await activities.doc(id).update(activity.toMap()); 
    } on Exception catch (e) { 
      print(e.toString()); 
    } 
  } 
if (widget.activity.id == null) { 
      await helper.insertActivity(widget.activity); 
    } 
    else { 
      await helper.updateActivity(widget.activity, widget.activity.id!); 
    }