Home > Software engineering >  What is the problem with this flutter code. Im trying to read the data from firebase
What is the problem with this flutter code. Im trying to read the data from firebase

Time:02-10

Future<Object?> GetData() async {
  FirebaseDatabase database = FirebaseDatabase.instance;

  DatabaseReference ref = FirebaseDatabase.instance.ref("litre");

// Get the data once
  DatabaseEvent event = await ref.once();

// Print the data of the snapshot
  print(event.snapshot.value);
  return event.snapshot.value;
}

Code is here I want to get the value of "litre" tag and then print it into Text() function. Firebase json is here =>

{
    "litre": "16"
}

And I expected to see "16" but I facing Instance of 'Future'

CodePudding user response:

GetData is an async function, so using it in a Text() widget will throw an exception because it is not string.

You can use FutureBuilder to build the Text() widget.

Example:

Otherwidgets,
FutureBuilder(
          future: GetData(),
          builder: (context, snapshot) => Text(snapshot.data.toString()),
        ),
  • Related