首页 > 解决方案 > The argument type 'String' can't be assigned to the parameter type 'Uri' 'Uri' is from 'dart:core'. final response = await http.post(url,

问题描述

when I run my flutter app I will get this kind of error.

The argument type 'String' can't be assigned to the parameter type 'Uri'.

here is my .dart file , code where the Error occured.

import 'dart:convert';
import 'dart:html';
import 'package:flutter/cupertino.dart';
import 'package:http/http.dart' as http;
import 'package:provider/provider.dart';
import 'package:save_geez/LoginAndSignupScreen/signup_page.dart';

class Authentication with ChangeNotifier {
  Future<void> Signup(String email, String password) async {
    const url =
        'https://identitytoolkit.googleapis.com/v1/accounts:signInWithCustomToken?key=AIzaSyAnhSx2zHOr0FO9qV-GBXizFg9sy4jz7dw';
    final response = await http.post(url,
        body: json.encode({
          'email': email,
          'password': password,
          "returnSecureToken": true,
        }));
    final responseData = json.decode(response.body);
    print(responseData);
  }
}

                                 ^

标签: flutterdart

解决方案


Please use this, it willsolve your problem:

Uri url =Uri.parse('https://identitytoolkit.googleapis.com/v1/accounts:signInWithCustomToken?key=AIzaSyAnhSx2zHOr0FO9qV-GBXizFg9sy4jz7dw');

The post method now uses a Uri object, and not a string. Same thing for get, put, delete..etc

Parse it as a Uri and pass the Uri object, as in the code snippet above. And keep everything else the same as it is, and things should work fine.


推荐阅读