Mohammad Zakir Raza
Mohammad Zakir Raza

Reputation: 135

Error 'A value of type 'dynamic' can't be assigned to a variable of type 'String'.'

A value of type 'dynamic' can't be assigned to a variable of type 'Map<String, dynamic>'. Try changing the type of the variable, or casting the right-hand type to 'Map<String, dynamic>'.

// Get Channel
var response = await http.get(uri, headers: headers);
if (response.statusCode == 200) {
  Map<String, dynamic> data = json.decode(response.body)['items'][0] ;
  Channel channel = Channel.fromMap(data);

Upvotes: 1

Views: 2171

Answers (1)

Rahul
Rahul

Reputation: 5049

json.decode(response.body) returns data of type Map<String, dynamic>.

If your response.body['items'][0] is also Map<String, dynamic>, you should cast it json.decode(response.body)['items'][0] as Map<String, dynamic>

Upvotes: 1

Related Questions