Beranda › Forums › Belajar Flutter Mobile Apps Tingkat Menengah › error apiurl mohon pencerahanya
- This topic has 4 replies, 2 voices, and was last updated 4 years, 7 months ago by sandi.
-
AuthorPosts
-
April 16, 2022 at 2:36 PM #49396
flutter run
Launching lib\main.dart on SM A013G in debug mode…
lib/post_result_model.dart:27:37: Error: The argument type ‘String’ can’t be assigned to the parameter type ‘Uri’.
– ‘Uri’ is from ‘dart:core’.
var apiResult = await http.post(apiURL, body: {“name”: name, “job”: job});
^FAILURE: Build failed with an exception.
* Where:
Script ‘C:\src\flutter\packages\flutter_tools\gradle\flutter.gradle’ line: 1102* What went wrong:
Execution failed for task ‘:app:compileFlutterBuildDebug’.
> Process ‘command ‘C:\src\flutter\bin\flutter.bat” finished with non-zero exit value 1* Try:
Run with –stacktrace option to get the stack trace. Run with –info or –debug option to get more log output. Run with –scan to get full
insights.* Get more help at https://help.gradle.org
BUILD FAILED in 27s
Running Gradle task ‘assembleDebug’… 28.5s
Exception: Gradle task assembleDebug failed with exit code 1April 16, 2022 at 2:38 PM #49397`import ‘dart:convert’;
import ‘package:http/http.dart’ as http;
class PostResult {
String id;
String name;
String job;
String created;PostResult(
{required this.id,
required this.name,
required this.job,
required this.created});factory PostResult.createPostResult(Map<String, dynamic> object) {
return PostResult(
id: object[“id”],
name: object[“name”],
job: object[“job”],
created: object[“createdAt”]);
}
static Future<PostResult> connectToAPI(String name, String job) async {
String apiUrl = “https://reqres.in/api/users”;var apiResult = await http.post(<strong>apiUrl</strong>, body: {“name”: name, “job”: job});
var jsonObject = json.decode(apiResult.body);return PostResult.createPostResult(jsonObject);
}
}April 16, 2022 at 2:43 PM #49398`import ‘dart:convert’;
import ‘package:http/http.dart’ as http;
class PostResult {
String id;
String name;
String job;
String created;PostResult(
{required this.id,
required this.name,
required this.job,
required this.created});factory PostResult.createPostResult(Map<String, dynamic> object) {
return PostResult(
id: object[“id”],
name: object[“name”],
job: object[“job”],
created: object[“createdAt”]);
}
static Future<PostResult> connectToAPI(String name, String job) async {
String apiUrl = “https://reqres.in/api/users”;var apiResult = await http.post(apiUrl, body: {“name”: name, “job”: job});
var jsonObject = json.decode(apiResult.body);return PostResult.createPostResult(jsonObject);
}
}
“error nya di bagian (apiUrl) tolong pencerahanya coach”April 16, 2022 at 3:03 PM #49399AnonymousHalo Sandi,
Kalau dilihat dari outputan errornyaError: The argument type ‘String’ can’t be assigned to the parameter type ‘Uri’. – ‘Uri’ is from ‘dart:core’. var apiResult = await http.post(apiURL, body: {“name”: name, “job”: job}); ^disini kita bisa tau bawah masalahnya ada di type data yang dimasukan ke parameter http.post ya, yang mana function tersebut menerima sebuah Uri bukan sebuah String. sedangkan variable apiURL kamu isinya sebuah string. Jadi untuk mengatasi error ini kamu harus ubah String tersebut menjadi sebuah Uri dulu ya, sehingga code kamu akan menjadi seperti ini:
final apiUrl = Uri.parse("https://reqres.in/api/users"); var apiResult = await http.post(apiUrl, body: {"name": name, "job": job});Btw, untuk cara pengubahan string jadi Uri ini ada di Praktik Rest API part II ya.
April 16, 2022 at 3:45 PM #49404baik coach saya coba dulu ya terima kasih
-
AuthorPosts
- You must be logged in to reply to this topic.