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

Take your career further with a raywenderlich.com Pro subscription. 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.

Add & Update Data into the Firestore Database Pro

This video was last updated on Aug 30 2022

In this lesson you will link your helper methods to the User Interface, and insert and update activities into the Firestore Database.

Contributors

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

You can unlock the rest of this video course, and our entire catalogue of books and videos, with a raywenderlich.com Professional subscription.

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!); 
    } 

Reviews

Comments