Search code examples
flutterflutter-dependencies

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


import 'package:crypto_app/Services/api_service.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  ApiService futurecontroller = Get.put(ApiService());
  List<Map<String, dynamic>> _data = [];

  Future<void> fetchalldata() async {
    final alldata = await futurecontroller.fetchData();
    print('I am ${alldata}');
    setState(() {
      _data = alldata;
      print('This is${_data}');
    });
  }

  @override
  void initState() {
    super.initState();
    fetchalldata();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Obx(
        () => futurecontroller.isLoading.value
            ? const Center(
                child: CircularProgressIndicator(
                  color: Colors.green,
                ),
              )
            : ListView.builder(
                itemCount: _data.length,
                itemBuilder: (context, index) {
                  final out = _data[index];
                  return Text(
                    out['RAW']['BTC']['INR']['FROMSYMBOL'].toString(),
                    style: const TextStyle(
                      color: Colors.green,
                    ),
                  );
                },
              ),
      ),
    );
  }
}

I am trying to fetch data from list _data and presenting it on test widgest and I am getting an error,Can anyone help me resolve this?

E/flutter ( 3418): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: type '_Map<String, dynamic>' is not a subtype of type 'List<Map<String, dynamic>>'
E/flutter ( 3418): #0      _HomePageState.fetchalldata.<anonymous closure> (package:crypto_app/Pages/homepage.dart:20:7)
E/flutter ( 3418): #1      State.setState (package:flutter/src/widgets/framework.dart:1138:30)
E/flutter ( 3418): #2      _HomePageState.fetchalldata (package:crypto_app/Pages/homepage.dart:19:5)
E/flutter ( 3418): <asynchronous suspension>

Above is the full context of error


Solution

  • The Problem

    The error indicates that you try to assign data of the type Map<String, dynamic> to a variable of type List<Map<String, dynamic>>.

    I assume a Map<String, dynamic> is returned by your function here:

    await futurecontroller.fetchData();
    

    Solutions

    The fix for this depends on what you are trying to achieve and how the bigger picture looks.

    Solution 1

    In case _data has to be of type List<Map<String, dynamic>> you can do something like:

    _data = [alldata];
    

    Solution 2

    In case _data doesn't need to be a List, you can change your definition:

    List<Map<String, dynamic>> _data = [];
    

    and update your UI code accordingly by replacing ListView.builder with:

    Text(
      _data['RAW']['BTC']['INR']['FROMSYMBOL'].toString(),
      style: const TextStyle(
        color: Colors.green,
      ),
    )