0

How can I correct this code? That might have sparked the need to win another solution.

enter image description here This result is only after a hot reload, and at the first start it is empty.

class PageOnes extends StatefulWidget {
  final String pageName;

  const PageOnes({
    required this.pageName,
    Key? key,
  }) : super(key: key);

  @override
  State<StatefulWidget> createState() => _PageOnesState();
}

class _PageOnesState extends State<PageOnes> {
  List<String> items = [];

  @override
  Widget build(BuildContext context)  {
    deviceInfo();

    return Scaffold(
      appBar: AppBar(
        title: Text(widget.pageName),
      ),
      body: ListView.separated(
          padding: const EdgeInsets.all(8),
          itemCount: items.length,
          itemBuilder: (context, index) {
            return ListTile(
              title: Text(items[index],style: const TextStyle(fontFamily: 'Proximal', fontSize: 18),
              ),
            );
          },
          separatorBuilder: (BuildContext context, int index) => const Divider(
                thickness: 5,
                endIndent: 10,
                indent: 10,
              )),
    );
  }

   void deviceInfo() async  {
    const int MEGABYTE = 1024 * 1024;
    DeviceInfoPlugin deviceInfoPlugin = DeviceInfoPlugin();
    AndroidDeviceInfo androidDeviceInfo = await deviceInfoPlugin.androidInfo;
    items.add('Brand: ${androidDeviceInfo.manufacturer}.');
     }
}

1 Answer 1

0

You are using future method, data isnt avialble on first frame , it will take some time to fetch data. Try using FutureBuilder

class _PageOnesState extends State<PageOnes> {
  Future<void> deviceInfo() async {
    List items = [];
    const int MEGABYTE = 1024 * 1024;
    DeviceInfoPlugin deviceInfoPlugin = DeviceInfoPlugin();
    AndroidDeviceInfo androidDeviceInfo = await deviceInfoPlugin.androidInfo;
    items.add('Brand: ${androidDeviceInfo.manufacturer}.');

    return items;
  }

  late final myFuture = deviceInfo();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.pageName),
      ),
      body: FutureBuilder(
        future: myFuture,
        builder: (context, snapshot) {
          if(snapshot.hasData){

            final items  = snapshot.data;

            return  ListView.separated(
            padding: const EdgeInsets.all(8),
            itemCount: items.length,
            itemBuilder: (context, index) {
              return ListTile(
                title: Text(
                  items[index],
                  style: const TextStyle(fontFamily: 'Proximal', fontSize: 18),
                ),
              );
            },
            separatorBuilder: (BuildContext context, int index) =>
                const Divider(
                  thickness: 5,
                  endIndent: 10,
                  indent: 10,
                )),
          }

          return CircularProgressIndicator();
        },
      ),
    );
  }
}

Not recommended but you can trick.

Dont call method directly inside build method. use intitState

  @override
  void initState() {
    super.initState();
    deviceInfo();
  }
 void deviceInfo() async  {
  .....
   setState((){});
     }
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much for the detailed answer and for resolving the issue. I tried the second option, but the first one will definitely analyze it really correctly.
Glad to help, try to follow 1st one. More on FutureBuilder

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.