Home > Software engineering >  How do I convert 'Future<String?>' to 'Future<String>?' in Flutter?
How do I convert 'Future<String?>' to 'Future<String>?' in Flutter?

Time:07-13

So basically Iam working with the enter image description here

You can either call it this way

FutureBuilder<String?>(
      future: info.getWifiName(),
      builder: (context, snapshot) {
        if (snapshot.hasData &&
            snapshot.connectionState == ConnectionState.done) {
          return Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                const Text(
                  'You are connected to WIFI ',
                ),
                Text(
                  '${snapshot.data}',
                  style: Theme.of(context).textTheme.headline4,
                ),
              ],
            ),
          );
        }
        return const Center(
          child: CircularProgressIndicator(),
        );
      })

or you can choose to not explicitly declare a data type when using FutureBuilder

FutureBuilder(
      future: info.getWifiName(),
      builder: (context, snapshot) {
        if (snapshot.hasData &&
            snapshot.connectionState == ConnectionState.done) {
          return Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                const Text(
                  'You are connected to WIFI ',
                ),
                Text(
                  '${snapshot.data}',
                  style: Theme.of(context).textTheme.headline4,
                ),
              ],
            ),
          );
        }
        return const Center(
          child: CircularProgressIndicator(),
        );
      })

Note that you aren't really converting the data type here, merely stating it and making the FutureBuilder aware of what data type to expect from the given future.

  • Related