Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save list getting from JSON URL in firestore collection

My small app, is getting list of users from JSON link then store it in the List, I wanna this list into usersCollection collection ref of firestore

usersCollection

my code

import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'dart:async';
import 'package:http/http.dart' as http;
import 'package:yat_flutter_app/main.dart';
import 'usersList.dart';
import 'package:cloud_firestore/cloud_firestore.dart';

class MyApp extends StatefulWidget {

  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {

  CollectionReference usersCollection =
  FirebaseFirestore.instance.collection('users');

  Future<List<User>> getUsers() async {
    var data = await http
        .get("https://www.json-generator.com/api/json/get/bYKKPeXRcO?indent=2");
    var jasonData = json.decode(data.body);
    List<User> users = [];

    for (var i in jasonData) {
      User user = User(i["index"], i["about"], i["name"], i["picture"],
          i["company"], i["email"]);
      users.add(user);
    }

    return users;
  }


  @override
  Widget build(BuildContext context) {

    List<User> usersList = getUsers() as List<User>;


    return Container(

      child: Column(
        children: [
          FutureBuilder(
            future: getUsers(),
            builder: (BuildContext context, AsyncSnapshot asyncSnapshop) {
              if (asyncSnapshop.hasData) {
                return Expanded(
                  child: ListView.builder(
                      shrinkWrap: true,
                      itemCount: asyncSnapshop.data.length,
                      itemBuilder: (BuildContext context, int index) {
                        return Card(
                          elevation: 5,
                          color: Colors.cyan[50],
                          child: ListTile(
                            trailing: Icon(Icons.share),
                            title: Text(asyncSnapshop.data[index].name, style: TextStyle(fontFamily: 'Tahoma',fontSize: 20,fontWeight: FontWeight.bold),),
                            leading: CircleAvatar(
                              backgroundImage: NetworkImage(
                                  asyncSnapshop.data[index].picture +
                                      asyncSnapshop.data[index].index.toString() +
                                      ".jpg"),
                            ),
                            subtitle: Text(asyncSnapshop.data[index].email,style: TextStyle(fontFamily: 'Tahmoma',fontSize: 18),),
                            onTap: (){
                              Navigator.push(context, new MaterialPageRoute(builder: (context)=>
                                  detailsPage(asyncSnapshop.data[index])
                              ));
                            },

                            onLongPress: ()=>

                                Fluttertoast.showToast(
                                    msg: asyncSnapshop.data[index].name,
                                    toastLength: Toast.LENGTH_SHORT,
                                    gravity: ToastGravity.CENTER,
                                    timeInSecForIosWeb: 1,
                                    backgroundColor: Colors.green[900],
                                    textColor: Colors.white,
                                    fontSize: 16.0
                                ),

                          ),
                        );
                      }),
                );
              } else {
                return Text("Loading, please wait...");
              }
            },
          ),

          ElevatedButton(
              child: Text('Save data'),
              onPressed: () => {
                
      usersCollection.add(getUsers()); // here's I am trying to add the result of getUsers into usersCollection 

              }),
        ],

      ),
    );
  }
}

like image 406
Indiana Evans Avatar asked Feb 06 '21 13:02

Indiana Evans


People also ask

How do I save a JSON file in firebase?

Open your browser, go to your Firebase console->Three Dots on the right->Import JSON. Important to note that when importing, whichever node you have selected in your Firebase database will be overwritten, so make sure you don't have your root node selected - create a child node and then do your import.

How do I get specific data from firestore?

There are two ways to retrieve data stored in Cloud Firestore. Either of these methods can be used with documents, collections of documents, or the results of queries: Call a method to get the data. Set a listener to receive data-change events.


2 Answers

To push this list to Firestore you need to fromJson and toJson methods in your model class

 factory User.fromJson(Map<String, dynamic> data){
    return User(
        index: data['index'] as int,
        about: data['about'] as String,
        name: data['name'] as String,
        picture: data['picture'] as String,
        company: data['company'] as String,
        email: data['email'] as String );
  }

  Map<String, dynamic> toJson(){

    return {
        "index": index,
        "about" : about,
        "name" : name,
        "picture" : picture,
        "company" : company,
        "email" : email,
    };
  }

instead that I would like to suggest using json_serializable library

then you need to do some changes in your future method like this

getUsers().then((users) {
// add users to map
});

and then you can use fromJson method to push it to firestore database

like image 74
Dr Mido Avatar answered Oct 12 '22 10:10

Dr Mido


To push an object to Firestore you need to convert your object to map.
You can just add this function to your class:

  Map<String, dynamic> toMap() {
    return {
      'field1': value1,
      'field2': value1,
    };
  }

To push a List , you need to convert all objects to map, you can do it with following method:

  static List<Map> ConvertToMap({List myList }) {
    List<Map> steps = [];
    myList.forEach((var value) {
      Map step = value.toMap();
      steps.add(step);
    });
    return steps;
  }

Or simply , see how to convert List to Map

I hope it will be useful

like image 27
Kabirou Agouda Avatar answered Oct 12 '22 10:10

Kabirou Agouda