Ad

Flutter 'map' Dynamic Call Of Null. Receiver: Instance Of '_Future' Arguments: [Instance Of '(dynamic) => Builder']

- 1 answer

i am trying to make a banner using this list which can be accessed with function "getBannerList" instead of having to manually write a list in this carousel

if i set items by manually putting a list at items: ['base64string','base64string','base64string'].map it will work fine but when i replace it with the function it causes this error

Error

The following NoSuchMethodError was thrown building MyApp(dirty, dependencies: [MediaQuery], state: _MyAppState#5a46f):
'map'
Dynamic call of null.
Receiver: Instance of '_Future<dynamic>'
Arguments: [Instance of '(dynamic) => Builder']

Code for carousel

Dependencies: carousel_slider: ^4.0.0

 CarouselSlider(
          options: CarouselOptions(
            height: MediaQuery.of(context).size.height * 0.15,
            autoPlay: true,
            autoPlayInterval: Duration(seconds: 5),
            initialPage: 0,
          ),
          items: getBannerList().map((e) {   // <---- if i replaced this with ['asdf','asdf,'asdf].map((e) it works fine
            return Builder(
              builder: (BuildContext context) {
                return Container(
                  width: MediaQuery.of(context).size.width,
                  margin: EdgeInsets.symmetric(horizontal: 5.0),
                  decoration: BoxDecoration(
                    image: DecorationImage(image: MemoryImage(base64Decode(e.toString()))),
                    color: Colors.white,
                  ),
                );
              },
            );
          }).toList(),
        ),

GetList Function

 getBannerList() async{
    dynamic data = await getDashBoardBannerData();
    print(data);
    return data;
  }

returns (example of return cause base64 string is too long):

['base64imgstring','base64imgstring','base64imgstring']
Ad

Answer

getBannerList method returns Future<dynamic> not List :
1- change method signature to Future<List<String>> getBannerList() async{}
2- define a variable in your StatefullWidget called

late final Future<List> bannerList;

3- initialize you variable in init state

@override
void initState(){
 bannerList = getBannerList();
}

4- now we should use FutureBuilder with our basserList

FutureBuilder<List<String>>(
        future: bannerList, // your future data
        builder: (BuildContext context, AsyncSnapshot<List<String>> snapshot) {
          if (snapshot.hasData) {
            return CarouselSlider(
          options: CarouselOptions(
            height: MediaQuery.of(context).size.height * 0.15,
            autoPlay: true,
            autoPlayInterval: Duration(seconds: 5),
            initialPage: 0,
          ),
          items: snapshot.data.map((e) {
            return Builder(
              builder: (BuildContext context) {
                return Container(
                  width: MediaQuery.of(context).size.width,
                  margin: EdgeInsets.symmetric(horizontal: 5.0),
                  decoration: BoxDecoration(
                    image: DecorationImage(image: MemoryImage(base64Decode(e.toString()))),
                    color: Colors.white,
                  ),
                );
              },
            );
          }).toList(),
        );
          } else if (snapshot.hasError) {
            return Text('Loading Error'); // error state
          } else {

          return CircularProgressIndicator(); // loading state
        },
      

you should use explicit types for fewer bugs.

Ad
source: stackoverflow.com
Ad