Mohamed Elsayed
Mohamed Elsayed

Reputation: 27

The argument type 'String' can't be assigned to the parameter type 'Object? Function(Object?, Object?)?'

I am using dart and have these two errors (The argument type 'String' can't be assigned to the parameter type 'Object? Function(Object?, Object?)?'.,The argument type 'JsonDecoder' can't be assigned to the parameter type 'Map<dynamic, dynamic>'.) in response.body and data

Main.dart

import 'dart:async';
import 'dart:convert' as convert;
import 'package:http/http.dart' as http;

void main() {
  Map post = new Map();

  Future<dynamic> getdata() async {
    var url = Uri.parse('https://jsonplaceholder.typicode.com/posts/1');
    var response = await http.get(url);
    if (response.statusCode == 200) {
      var data = convert.JsonDecoder(response.body);
      post.addAll(data);
    }
  }

  print('please wait..');
  getdata();
  print('Your data is $post');
}
 

Upvotes: 0

Views: 453

Answers (1)

DJ Hemath
DJ Hemath

Reputation: 662

Use jsonDecode instead of convert.JsonDecoder. Because JsonDecoder returns an instance of itself. Since your variable post is of type Map, this can't be added to it. If you use jsonDecode, it will return you a Map. And this is the reason for the first error too.

You're trying to print the instance of JsonDecoder as a string.

check the following,

import 'dart:async';
import 'dart:convert';
import 'package:http/http.dart' as http;

void main() {
  Map<String, dynamic> post = new Map();

  Future<dynamic> getdata() async {
    var url = Uri.parse('https://jsonplaceholder.typicode.com/posts/1');
    var response = await http.get(url);
    if (response.statusCode == 200) {
      var data = jsonDecode(response.body);
      post.addAll(data);
    }
  }

  print('please wait..');
  getdata();
  print($post);
}
 

Upvotes: 1

Related Questions