0

I'm trying to display value of Future<List> with flutter in a text of an alert dialog but I got instance of future<list>. this is my code :

WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
                showDialog(
                    context: context,
                    barrierColor: Colors.transparent,
                    builder: (context) {
                      return AlertDialog(
                        title: Text(
                            'Be Careful ${characteristic46?.read()}'),
                        backgroundColor: Color.fromARGB(45, 231, 148, 54),
                        alignment: Alignment.topCenter,
                      );
                    });
              });

Thanks in advance for your help

rania
  • 63
  • 6

1 Answers1

1

Your characteristic46?.read() is Future and you need to await for its result. Try this:

Future<void> showFutureDialog(BluetoothCharacteristic? characteristic46) async{
    var result = await characteristic46?.read();
    WidgetsBinding.instance.addPostFrameCallback((timeStamp){
            showDialog(
                context: context,
                barrierColor: Colors.transparent,
                builder: (context) {
                  return AlertDialog(
                    title: Text(
                        'Be Careful $result'),
                    backgroundColor: Color.fromARGB(45, 231, 148, 54),
                    alignment: Alignment.topCenter,
                  );
                });
          });
}

then call showFutureDialog inside initState:

@override
  void initState() {
    super.initState();

    showFutureDialog(characteristic46);
  }
eamirho3ein
  • 16,619
  • 2
  • 12
  • 23
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/249804/discussion-between-rania-and-eamirho3ein). – rania Nov 22 '22 at 09:10