Beranda › Forums › Belajar Flutter Mobile Apps Tingkat Menengah › running sudah tidak ada error tapi ada notif
- This topic has 6 replies, 2 voices, and was last updated 4 years, 6 months ago by Anonymous.
-
AuthorPosts
-
April 21, 2022 at 12:19 PM #49531
notif nya seperti ini coach
Exception has occurred.
FormatException (FormatException: Unexpected character (at character 1)
<!DOCTYPE html>
^
)lalu di sebelah widget inspector ada convert_patch.dart
1405 throw new FormatException(message, chunk, position);
ada tulisan itu d stabilo kuning
kalo kodingan nya raunning ngga ada error cuma fungsi get nya tidak mengeksekusi apa2 (tidak jalan) lalu muncul notif seperti yg saya
jelaskan di atas
terima kasih coach mohon bantuanyaApril 22, 2022 at 8:41 AM #49570AnonymousHi Sandi,
Boleh tolong dikirimikan hasil dari response get nya kalau dibuka dari website atau postman?
karena kalau dilihat sekilas dari log error ini<!DOCTYPE html>sepertinya ada yang salah dengan api yang kamu hit
hasil balikan/response dari API yang kamu hit harus berupa sebuah JSON.April 22, 2022 at 7:44 PM #49595maksudnya respon pada saat di klik butto Get nya coach?
sebelum nya ini kodingan main dart nyaimport ‘package:api_demo/post_result_model.dart’;
import ‘package:api_demo/user_model.dart’;
import ‘package:flutter/material.dart’;void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}class _MyAppState extends State<MyApp> {
PostResult postResult = null;
User user = null;
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
appBar: AppBar(
flexibleSpace: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [Colors.blue, Colors.purple],
begin: FractionalOffset.topCenter,
end: FractionalOffset.bottomCenter)),
),
title: Text(
“API DEMO”,
style: TextStyle(fontSize: 20, fontWeight: FontWeight.w700),
),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Text((user != null)
? user.id + “|” + user.name
: “tidak ada data”),
RaisedButton(
onPressed: () {
User.connectAPI(“2”).then((value) {
user = value;
setState(() {});
});
},
child: Text(“get”),
)
],
),
),
),
);
}
}April 22, 2022 at 7:46 PM #49596kodingan pos_result_model. dart nya
import ‘dart:convert’;
import ‘package:http/http.dart’ as http;class PostResult {
String id;
String name;
String job;
String created;PostResult({this.id, this.name, this.job, 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 {
final apiUrl = Uri.parse(“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);
}
}April 22, 2022 at 7:47 PM #49597ini kodingan user_modelnya. dart nya mohon petunjuknya coach barang kali ada yg salah dari kodingan nya
import ‘dart:convert’;
import ‘package:http/http.dart’ as http;class User {
String id;
String name;User({this.id, this.name});
factory User.createUser(Map<String, dynamic> object) {
return User(
id: object[“id”].toString(),
name: object[“first_name”] + ” ” + object[“last_name”]);
}
static Future<User> connectAPI(String id) async {
final apiUrl = Uri.parse(“https://reqres.in//api/users/2” + id);var apiResult = await http.get(apiUrl);
var jsonObject = json.decode(apiResult.body);
var userData = (jsonObject as Map<String, dynamic>)[‘data’];return User.createUser(userData);
}
}April 22, 2022 at 7:55 PM #49598running well udah di debuging tapi begitu di klik button get nya ngga ngambil data apa2
tapi malah muncul convert_patch.dartNever fail(int position, [String? message]) {
if (message == null) {
message = “Unexpected character”;
if (position == chunkEnd) message = “Unexpected end of input”;
}
throw new FormatException(message, chunk, position);
}
}
bagian throw new formatexception(message, chunk, position); nya kaya di stabilo kuningterima kasi coach
April 25, 2022 at 7:18 PM #49659AnonymousHi Sandi, masalahnya itu ada pada api yang kamu hit nih,
Hasil balikan dari api tersebut bukan merupakan sebuah json.
Dari yang saya coba, url yang kamu hit salah, jadi dapatnya error 404 not found, sehingga membalikan isi body yang merupakan isi html makanya error.
Jadi pastikan kamu baca lagi dokumentasi dari api yang kamu hit, atau kamu bisa ngelakuin http get/post dengan postman terlebih dahulu untuk memastikan urlnya benar dan memberikan response sebuah JSON. -
AuthorPosts
- You must be logged in to reply to this topic.