1

I am using http: ^0.13.3, Here is my simple code and when i am running this code i got error: "Expected a value of type ‘Map<String, dynamic>', but got one of type ‘List' occured" and i don't know why i am getting this error. If there is any other way to doing that please tell me.

class SubCategoryObject {
  final int id;
  final String category;

  SubCategoryObject({@required this.id, @required this.category});

  factory SubCategoryObject.fromJson(Map<String, dynamic> json) {
    return SubCategoryObject(
      id: json['id'],
      category: json['title'],
    );
  }
}
class SubCategoryListScreen extends StatelessWidget {
  static const route = 'sub-category-list';
  SubCategoryListScreen({Key key}) : super(key: key);

  Future<SubCategoryObject> getData() async {
    final res =
        await http.get(Uri.https("jsonplaceholder.typicode.com", "todos"));

    if (res.statusCode == 200) {
      return SubCategoryObject.fromJson(jsonDecode(res.body));
    } else {
      // If that call was not successful, throw an error.
      throw Exception('Failed to load ');
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("martman"),
      ),
      body: FutureBuilder<SubCategoryObject>(
        future: getData(),
        builder: (ctx, snapshot) {
          // Checking if future is resolved or not
          if (snapshot.connectionState == ConnectionState.done) {
            // If we got an error
            if (snapshot.hasError) {
              return Center(
                child: Text(
                  '${snapshot.error} occured',
                  style: TextStyle(fontSize: 18),
                ),
              );

              // if we got our data
            } else if (snapshot.hasData) {
              return Text(snapshot.data.category);
            }
          }

          // Displaying LoadingSpinner to indicate waiting state
          return Center(
            child: CircularProgressIndicator(),
          );
        },

        
      ),
    );
  }
}

1
  • 1
    print & share the screenshot of the data that you are getting from api. I believe it is a list and not a map. Commented May 6, 2021 at 18:33

1 Answer 1

4

Please check the following link. it returns List, not Map data. https://jsonplaceholder.typicode.com/todos

Future<List<SubCategoryObject>> getData() async {
    final res =
        await http.get(Uri.https("jsonplaceholder.typicode.com", "todos"));

    if (res.statusCode == 200) {
      dynamic result = jsonDecode(res.body);
      return (result as List).map((e) => SubCategoryObject.fromJson(e)).toList();
    } else {
      // If that call was not successful, throw an error.
      throw Exception('Failed to load ');
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.