简体   繁体   中英

Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'List<dynamic>'

I try to fetch data from apis to my Flutter app and i get this error. I want to display some things in my home page with my getDashboardItems function after i log in.

network.dart

Future<List> getDashboardItems() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    var accessToken = "Token ${prefs.getString("accessToken")!}";
    final response = await get(
      Uri.parse('$baseUrlApi/employee-api/dashboard/'),
      headers: <String, String>{
        'Content-Type': 'application/json; charset=UTF-8',
        'Authorization': accessToken,
      },
    );

repository.dart

Future<List> getDashboardItem() async {
    final signUpData = await networkService.getDashboardItems();
    if (signUpData[0] == true) {
      try {
        return [true, signUpData[1]];
      } catch (e) {
        return [false, []];
      }
    }
    return [false, signUpData[1]];
  }

homecontroller.dart

getDashboardItem(context) {
    isLoading.value = true;
    repository.getDashboardItem().then((res) {
      if (res[0] == true) {
        isLoading.value = false;
        getDashboardResponse.value = res[1];
      } else if (res[1]['detail'] == 'Invalid token.' ||
          res[1]['detail'] == 'Authentication credentials were not provided') {
        Get.snackbar('Error', 'Your session is out, Please login again',
            snackPosition: SnackPosition.TOP);
        logout(context);
      } else {
        isLoading.value = false;
        Get.snackbar('Error', res[1]['parameter error'],
            snackPosition: SnackPosition.TOP);
      }
    });
  }

When I get this error it sends my on the line

        getDashboardResponse.value = res[1];
 

from homecontroller.

Its because you are trying to do this for example {"data":[]}(ie) this is your response data so res[1] is not possible to get a better understandment of what you are trying to best is it possible to share the response you are getting from the api

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM