Upgrade Skill Di ITBOX Jadi Lebih Hemat
Special Diskon 50%
Sampai 16 November 2026!
Hari
Jam
Menit
Detik
GARUDABOX

error apiurl mohon pencerahanya

Beranda › Forums › Belajar Flutter Mobile Apps Tingkat Menengah › error apiurl mohon pencerahanya

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #49396
    sandi
    Participant

      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 1

      #49397
      sandi
      Participant

        `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&#8221;;

        var apiResult = await http.post(<strong>apiUrl</strong>, body: {“name”: name, “job”: job});
        var jsonObject = json.decode(apiResult.body);

        return PostResult.createPostResult(jsonObject);
        }
        }

        #49398
        sandi
        Participant

          `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”

          #49399
          Anonymous

            Halo Sandi,
            Kalau dilihat dari outputan errornya

            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});
            ^

            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.

            #49404
            sandi
            Participant

              baik coach saya coba dulu ya terima kasih

            Viewing 5 posts - 1 through 5 (of 5 total)
            • You must be logged in to reply to this topic.
            Shopping cart0
            There are no products in the cart!
            Continue shopping