Home > Back-end >  How to use Future<String?> object inside of drawer menu?
How to use Future<String?> object inside of drawer menu?

Time:05-24

Well, my question is simple.I have one future string (sharedpreferences value) and I would like to use it in drawer menu.

However, when I tap drawer menu I get either initialization error or text "instance of Future<String?>".

drawer codes

import 'package:flutter/material.dart';

class UserInformation extends StatelessWidget {
  const UserInformation({
    Key? key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return const UserAccountsDrawerHeader(
      margin: EdgeInsets.zero,
      decoration: BoxDecoration(
          color: Colors.white,
          border: Border(
            top: BorderSide(width: 3.0, color: Color(0xFF166FC0)),
            left: BorderSide(width: 3.0, color: Color(0xFF166FC0)),
            right: BorderSide(width: 3.0, color: Color(0xFF166FC0)),
            bottom: BorderSide(width: 0),
          )),
      accountName: Text(
        "KULLANICI ADI",
        style: TextStyle(
            fontSize: 24,
            fontWeight: FontWeight.bold,
            color: Color(0xFF166FC0)),
      ),
      accountEmail: Text(
        "[email protected]",
        style: TextStyle(
            fontSize: 14,
            fontWeight: FontWeight.bold,
            color: Color(0xFF0FA9EA)),
      ),
      currentAccountPicture: CircleAvatar(
        radius: 40,
        backgroundColor: Color(0xFF0FA9EA),
        child: CircleAvatar(
          radius: 32,
          backgroundColor: Colors.white,
          child: Text(
            "AS",
            style: TextStyle(
                fontWeight: FontWeight.bold,
                fontSize: 40,
                color: Color(0xFF166FC0)),
          ),
        ),
      ),
    );
  }
}

my adorable future string:

  static Future<String?> get getEmail async {
    final prefs = await SharedPreferences.getInstance();
    String? email = await prefs.getString('email');
    return email;
  }

CodePudding user response:

I would recommend using the FutureBuilder class. It has support for error handling and a 'loading value' when the future isn't completed yet.

CodePudding user response:

wrap your Text Widget into a FutureBuilder

https://api.flutter.dev/flutter/widgets/FutureBuilder-class.html

  • Related