Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I'm getting this error "The operator '[]' isn't defined for the type 'Future<dynamic>'." can someone please help me fix my code? flutter/dart

I am getting these two errors when building a listview.

The getter ‘length’ isn’t defined for the type ‘Future’.
The operator ‘[]’ isn’t defined for the type ‘Future’.

whats going wrong here and is there any fix to my code.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Thanks Alot!

This is my code :

import 'package:cache_manager/cache_manager.dart';
import 'package:flutter/material.dart';



class messageBoard extends StatefulWidget {
  messageBoard({Key key}) : super(key: key);

  @override
  State<messageBoard> createState() => _messageBoardState();
}

class _messageBoardState extends State<messageBoard> {

  @override
  Widget build(BuildContext context) {

      var message = ReadCache.getStringList(key: "cahce3");

      return Scaffold(
        backgroundColor: Colors.white,
        body: Column(
          children: [
            Expanded(
              child: FutureBuilder(
                builder: (context , snapshot) {
                  return ListView.builder(
                      itemCount: message.length,
                      itemBuilder:(context,index){
                        return Text(message[index]);
                      }
                  );
                },
              ),
            )],
        ),
      );
    }
  }

>Solution :

Future builder takes a future. You can assign the future to Futurebuilder and check its data. Assuming ReadCache.getStringList is a future you can write it like this.

FutureBuilder(
    future: ReadCache.getStringList(key: "cahce3"),
         builder: (context , snapshot) {
            if (snapshot.hasData) {     
                  return ListView.builder(
                      itemCount: snapshot.data.length,
                      itemBuilder:(context,index){
                        return Text(snapshot.data[index]);
                      }
                  );
                }
             else{ 
               return Text("No Data");
             }
          },
      ),
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading