Home > Software engineering >  API and HTTP flutter data
API and HTTP flutter data

Time:02-16

How to Fix this error enter image description here Error: The argument type 'String' can't be assigned to the parameter type 'Uri'.

  • 'Uri' is from 'dart:core'. await http.get('https://jsonplaceholder.typicode.com/posts/1');

Error: Property 'title' cannot be accessed on 'Post?' because it is potentially null.

  • 'Post' is from 'package:networking/model/post.dart' ('lib/model/post.dart'). Try accessing using ?. instead. return Text(snapshot.data.title); ^^^^^ lib/model/post.dart:11:17: Error: The operator '[]' isn't defined for the class 'JsonCodec'.
  • 'JsonCodec' is from 'dart:convert'. Try correcting the operator to an existing operator, or defining a '[]' operator. id: json['id'],

CodePudding user response:

You need to use Uri.parse() for parse url of api like

http.get(
          Uri.parse('${AppConfig.apiBaseUrl}country'),
        );

CodePudding user response:

http does not handle the URL strings directly starting from http: 0.13.0.

You need to use either.

http.get(Uri.https(AppConfig.apiBaseUrl, 'country'));

or

http.get(Uri.parse('${AppConfig.apiBaseUrl}country'));
  • Related