【问题标题】:Uri' is from 'dart:core' [duplicate]Uri'来自'dart:core' [重复]
【发布时间】:2021-08-24 13:42:47
【问题描述】:

bin/dart_application_1.dart:18:74:错误:不能将参数类型“字符串”分配给参数类型“Uri”。 - 'Uri' 来自 'dart:core'。等待 http.get("https://jsonplaceholder.typicode.com/users/$userId");

import 'dart:convert';

void main(List<String> arguments) {
  fetchUserId();
}

void fetchUserId() async {
  print('before');
  var result = await fetchUser(5);
}

// future
Future<Map<String, String>> fetchUser(int userId) async {
  // return Future.delayed(
  //     Duration(seconds: 5), () => {'id': userId.toString(), 'name': 'Max'});
  var responce =
      await http.get('https://jsonplaceholder.typicode.com/users/$userId');
  var map = json.decode(responce.body) as Map;
  return {'id': map['id'].toString(), 'name': map['name']};
}

【问题讨论】:

    标签: flutter dart


    【解决方案1】:
    import 'dart:convert';
    
    void main(List<String> arguments) {
      fetchUserId();
    }
    
    void fetchUserId() async {
      print('before');
      var result = await fetchUser(5);
    }
    
    // future
    Future<Map<String, String>> fetchUser(int userId) async {
      // return Future.delayed(
      //     Duration(seconds: 5), () => {'id': userId.toString(), 'name': 'Max'});
      var responce =
          await http.get(Uri.parse('https://jsonplaceholder.typicode.com/users/$userId'));
      var map = json.decode(responce.body) as Map;
      return {'id': map['id'].toString(), 'name': map['name']};
    }
    

    【讨论】:

      猜你喜欢
      • 2021-07-06
      • 2021-07-31
      • 2013-07-22
      • 2011-05-30
      • 2011-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多