Home > Software design >  How to fix not a subtype error in Flutter
How to fix not a subtype error in Flutter

Time:08-01

I'm trying to use this alphabet scroll package in my Flutter project but I'm getting this error and not sure how to fix this so I would be really appreciated if I can get any help or suggestion.

type 'Future<dynamic>' is not a subtype of type 'List<AlphaModel>'
The relevant error-causing widget was
Consumer<MainController>

I have tried doing like this but I'm getting "Class 'Future' has no instance method 'map'.

  list: data.getSawnawkJsonData().map((e) => AlphaModel(e)).toList(),

MainController.dart

  getSawnawkJsonData() async {
    final jsondata =
        await rootBundle.rootBundle.loadString('assets/data/sawnawk_data.json');
    final list = json.decode(jsondata) as List<dynamic>;
    for (var i = 0; i < list.length; i  ) {
      sawnawkItems.add(SawnAwkModel.fromJson(list[i]));
    }
    notifyListeners();
  }

Detail.dart

      body: provider.Consumer<MainController>(
        builder: (context, data, child) {
          if (data.sawnawkItems.isEmpty) {
            data.getSawnawkJsonData();
            return const Center(child: CircularProgressIndicator());
          }
          if (data.sawnawkItems.isNotEmpty) {
            return AlphabetScrollView(
              list: data.getSawnawkJsonData(),
              alignment: LetterAlignment.right,
              itemExtent: 50,
              unselectedTextStyle: TextStyle(
                  fontSize: 15,
                  fontWeight: FontWeight.normal,
                  color: Colors.black),
              selectedTextStyle: TextStyle(fontSize: 20, color: Colors.blue),
              itemBuilder: (_, k, id) {
                return SawnawkCardWidget(
                  id: data.sawnawkItems[k].id!,
                  pageNumber: data.sawnawkItems[k].pageNumber,
                  titleFalam: data.sawnawkItems[k].titleFalam,
                  bookmark: data.sawnawkItems[k].bookmark,
                  sawnawkNumber: data.sawnawkItems[k].sawnawkNumber,
                  titleEnglish: data.sawnawkItems[k].titleEnglish,
                  isVisible: false,
                );
              },
            );
          }
          return const Center(
              child: Text("Error Happend Try Refresh The Page"));
        },
      ),
[
    {
        "id": 0,
        "pageNumber": 499,
        "sawnawkNumber": "1",
        "titleFalam": "KUM THAR THU",
        "titleEnglish": "(New Year)",
        "favorite": false
    },
    {
        "id": 1,
        "pageNumber": 500,
        "sawnawkNumber": "2",
        "titleFalam": "PATHIAN IH UMPINAK",
        "titleEnglish": "(God's Presence)",
        "favorite": false
    },

AlphaModel


class AlphaModel {
  final String key;
  final String? secondaryKey;

  AlphaModel(this.key, {this.secondaryKey});
}

CodePudding user response:

First of all, your function is async, so you need to use await keyword:

final jsonData = await data.getSawnawkJsonData() as List;

after that, you can use the "map" function if you are sure your JSON data is a List.

jsonData.map((e) => AlphaModel(e)).toList();

CodePudding user response:

You can use like this:

Future<dynamic> getSawnawkJsonData() async {
    String url = 'the URL API method';
    return await _apiService.makeApiRequest(
        method: get, url: url, body: null, headers: null);
  }

While at the time of adding data into the list:

getSawnawkJsonData().then((){
   sawnawkItems = value;
})

you can use this method to get data from API and append it in your list

CodePudding user response:

I will recommend using FutureBuilder here. Because getSawnawkJsonData is Future and AlphabetScrollView's 'list' expect a list of string.

You need to return a list of string from getSawnawkJsonData.

Do it like

Future<List<String>>  getSawnawkJsonData() async {
   
/// your operation

return  ["A",]; //your data
}

This is how it will be structured

FutureBuilder(
  future: data.getSawnawkJsonData(),
  builder: (context, snapshot) {
  if (snapshot.hasData) {
    return AlphabetScrollView(
      list: snapshot.data,
      itemExtent: 50,
      unselectedTextStyle: TextStyle(
          fontSize: 15,
          fontWeight: FontWeight.normal,
          color: Colors.black),
      selectedTextStyle: TextStyle(fontSize: 20, color: Colors.blue),
      itemBuilder: (_, k, id) {},

Also i will recommend creating future variable instead of directly providing on future on StatefulWidget Check this video by Randal L. Schwartz

  • Related