roee attias
roee attias

Reputation: 183

How to get and print map data from a function (Flutter)

I wrote a function that get data from firebase and storing it in a map structure, And know I want to call that function from another code and get the map and print all of the fields. But I don't know what is the syntax for my will. If someone know how can I do it it will be fantastic.

this is the function:

import 'package:cloud_firestore/cloud_firestore.dart';

Future<Map<String, dynamic>> getData () async {
  Map<String, dynamic> data = {
    "first-name": "",
    "last-name": "",
    "phone-number": "",
    "shop-name": "",
    "work-start": "",
    "work-end": "",
    "location": "",
  };
  firestore = FirebaseFirestore.instance;  
  DocumentSnapshot userPersonalData = await firestore!.collection("Users").doc("TvIRbwzfrebPj10Cubc0RpnD4Y83").collection("info").doc("personal-dits").get();
  DocumentSnapshot userShopName = await firestore!.collection("Users").doc("TvIRbwzfrebPj10Cubc0RpnD4Y83").collection("info").doc("shop-info").get();
  data.update("first-name", (value) => userPersonalData["first-name"]);
  data.update("last-name", (value) => userPersonalData["last-name"]);
  data.update("phone-number", (value) => userShopName["Phone number"]);
  data.update("shop-name", (value) => userShopName["shop name"]);
  data.update("work-start", (value) => userShopName["work-start"]);
  data.update("work-end", (value) => userShopName["work-end"]);
  data.update("location", (value) => userShopName["location"]);
  return data;
}

and here is my other file (the one that call the function):

class ProfileC extends StatefulWidget {
  const ProfileC({Key? key}) : super(key: key);

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

class _ProfileCState extends State<ProfileC> {

  Future<Map<String, dynamic>>? data;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          TextButton(
            onPressed: () {
              data = getData();
              // in this line I want to print one of the fields
            },
            child: Text("test"),
          )
        ],
      ),
    );
  }
}

Upvotes: 1

Views: 1834

Answers (1)

Salih Can
Salih Can

Reputation: 1821

Below code should be works

Future<Map<String, dynamic>> getData () async {
  Map<String, dynamic> data = {};
  firestore = FirebaseFirestore.instance;
  DocumentSnapshot userPersonalData = await firestore!.collection("Users").doc("TvIRbwzfrebPj10Cubc0RpnD4Y83").collection("info").doc("personal-dits").get();
  DocumentSnapshot userShopName = await firestore!.collection("Users").doc("TvIRbwzfrebPj10Cubc0RpnD4Y83").collection("info").doc("shop-info").get();
  data["first-name"] = userPersonalData["first-name"];
  data["last-name"] = userPersonalData["last-name"];
  data["phone-number"] = userShopName["Phone number"];
  data["shop-name"] = userShopName["shop name"];
  data["work-start"] =  userShopName["work-start"];
  data["work-end"] = userShopName["work-end"];
  data["location"] = userShopName["location"];
  return data;
}
class ProfileC extends StatefulWidget {
  const ProfileC({Key? key}) : super(key: key);

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

class _ProfileCState extends State<ProfileC> {

  Future<Map<String, dynamic>>? data;
  
  @override
  void initState() {
    _getDataAndPrint();
    super.initState();
  }
  
  Future<void> _getDataAndPrint() async {
    data = await getData();
    print('FirstName: ${data!["first-name"]}');
  }
  

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          TextButton(
            onPressed: () {},
            child: Text("test"),
          )
        ],
      ),
    );
  }
}

Upvotes: 2

Related Questions