When I tried to get data from API by Models I found this error in my debug console..
**I/flutter (31507): type 'List<dynamic>' is not a subtype of type 'Iterable<Connection>'
**
How can I get data from this model? without a model I can't show the API data in StreamBuilder, without a Model I easily get the data to show in my app.
API Demo :
[
{
"id": 1,
"confrm_date": "2022-01-17T07:14:06",
"User_name": "গণপরিবহনে অঘোষিত নিয়ম",
"Phone_Number": "01854555",
"Description": "পরিবহন খাতের সঙ্গে মো. রুবেল হোসেন যুক্ত সেই ১৯৯৭ সাল থেকে। শুরু থেকেই তিনি ‘পুলিশ পাস’-এর কথা শুনে আসছেন। আদৌ এ নিয়ে কোনো আইন বা নিয়ম আছে কি না, তা জানেন না তিনি। তাঁর মতো অনেক পরিবহনমালিক ও শ্রমিকের একই অবস্থা।\r\n\r\nএত দিন শিক্ষার্থীদের জন্য বাসে অর্ধেক ভাড়ার (হাফ পাস) কথা শোনা গেলেও গণপরিবহনে ভাড়াসংক্রান্ত আরেকটি বিষয় প্রচলিত আছে। সেটিই হলো ওই ‘পুলিশ পাস’। এ ক্ষেত্রে অর্ধেক ভাড়া নয়, কোনো ভাড়াই দিচ্ছেন না অনেক পুলিশ সদস্য।",
"Connection_status": false,
"cat_img": "http://103.137.75.74:82/media/cat_img/LOGO-01.png",
"Connection_by": 1
},
{
"id": 2,
"confrm_date": "2022-01-17T07:14:06",
"User_name": "অবৈধ রসিদে টোল",
"Phone_Number": "01854555",
"Description": "অবৈধ রসিদ তৈরি করে রাজধানীর যাত্রাবাড়ীর মেয়র হানিফ উড়ালসড়ক থেকে টোল আদায় করার অভিযোগ উঠেছে সংঘবদ্ধ একটি চক্রের বিরুদ্ধে। বিভিন্ন যানবাহনের চালকদের ভয় দেখিয়ে টোলের নামে চাঁদা আদায় করছিল চক্রটি। না দিলে প্রাণনাশের হুমকি দিচ্ছিল তারা।",
"Connection_status": false,
"cat_img": "http://103.137.75.74:82/media/cat_img/LOGO-01.png",
"Connection_by": 1
},
{
"id": 3,
"confrm_date": "2022-01-17T10:42:36",
"User_name": "User",
"Phone_Number": "01854555",
"Description": "নরমাল ডেলিভারি হসপিটাল",
"Connection_status": false,
"cat_img": "http://103.137.75.74:82/media/cat_img/LOGO-01.png",
"Connection_by": 1
},
{
"id": 4,
"confrm_date": "2022-01-17T10:42:46",
"User_name": "User",
"Phone_Number": "01854555",
"Description": "কাশেম মঞ্জিল, তানযীমুল উম্মাহ হিফয মাদ্রাসা।",
"Connection_status": false,
"cat_img": "http://103.137.75.74:82/media/cat_img/LOGO-01.png",
"Connection_by": 1
},
{
"id": 5,
"confrm_date": "2022-01-17T10:42:57",
"User_name": "User",
"Phone_Number": "78339399",
"Description": "হাউজিং মারকাজ মসজিদের পিছনে - nstu teacher 2300 tk router \r\nline charge 1000 360",
"Connection_status": false,
"cat_img": "http://103.137.75.74:82/media/cat_img/LOGO-01.png",
"Connection_by": 1
},
{
"id": 6,
"confrm_date": "2022-01-17T10:43:04",
"User_name": "User",
"Phone_Number": "01854555",
"Description": "মাষ্টার পাড়া - হালিমা ভিলা ৩ তলায়",
"Connection_status": false,
"cat_img": "http://103.137.75.74:82/media/cat_img/LOGO-01.png",
"Connection_by": 1
},
{
"id": 7,
"confrm_date": "2022-01-17T10:44:25",
"User_name": "User",
"Phone_Number": "0185455545",
"Description": "মিঠুর কানেকশন আছে এখন , ১ তারিখ নিবে।",
"Connection_status": false,
"cat_img": "http://103.137.75.74:82/media/cat_img/LOGO-01.png",
"Connection_by": 1
},
{
"id": 8,
"confrm_date": "2022-01-17T10:44:49",
"User_name": "User",
"Phone_Number": "01855545",
"Description": "এতিম খানার পিছনে।",
"Connection_status": false,
"cat_img": "http://103.137.75.74:82/media/cat_img/LOGO-01.png",
"Connection_by": 1
}
]
My Model is:
class Connection {
int? id;
String? confrmDate;
String? userName;
String? phoneNumber;
String? description;
bool? connectionStatus;
String? catImg;
int? connectionBy;
Connection(
{this.id,
this.confrmDate,
this.userName,
this.phoneNumber,
this.description,
this.connectionStatus,
this.catImg,
this.connectionBy});
Connection.fromJson(Map<String, dynamic> json) {
id = json['id'];
confrmDate = json['confrm_date'];
userName = json['User_name'];
phoneNumber = json['Phone_Number'];
description = json['Description'];
connectionStatus = json['Connection_status'];
catImg = json['cat_img'];
connectionBy = json['Connection_by'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['id'] = this.id;
data['confrm_date'] = this.confrmDate;
data['User_name'] = this.userName;
data['Phone_Number'] = this.phoneNumber;
data['Description'] = this.description;
data['Connection_status'] = this.connectionStatus;
data['cat_img'] = this.catImg;
data['Connection_by'] = this.connectionBy;
return data;
}
}
** ListViewBuilder:**
class NewCollectionTab extends StatefulWidget with BaseController {
@override
State<NewCollectionTab> createState() => _NewCollectionTabState();
}
class _NewCollectionTabState extends State<NewCollectionTab>
with BaseController {
final keyRefresh = GlobalKey<RefreshIndicatorState>();
@override
Widget build(BuildContext context) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
SizedBox(height: 20),
Padding(
padding: EdgeInsets.symmetric(horizontal: 20),
child: KText(
text: 'Main',
fontSize: 18,
fontWeight: FontWeight.w600,
color: Colors.black,
),
),
SizedBox(height: 10),
ListView.builder(
shrinkWrap: true,
primary: false,
physics: BouncingScrollPhysics(),
itemCount: connectionDataC.connection.length,
itemBuilder: (context, index) {
final item = connectionDataC.connection[index];
return InkWell( child: Padding(
padding: EdgeInsets.only(bottom: 10),
child: ListTile(
leading: CircleAvatar(
radius: 30,
backgroundColor: black12,
backgroundImage: NetworkImage(
item!.catImg.toString(),
),
), ),
),
);
},
),
],
);
}
}
My Controller is :
class ConnectionController extends GetxController {
final _dio = Dio();
final connection = RxList<Connection>();
//
getconnectionList() async {
print(' ');
try {
final sharedPreferences = await SharedPreferences.getInstance();
final token = sharedPreferences.get('accessToken');
final userName = sharedPreferences.get('loginUserName');
final res = await _dio.get(
'$baseUrl/new_connection/?search=$userName',
options: Options(
headers: {
"Content-Type": "application/json",
"Accept": "application/json",
"Authorization": 'Token $token'
},
),
);
print(res.statusCode);
print(res.headers);
// print(res.body);
// print(res.unauthorized);
print(res.realUri);
print(res.statusMessage);
// print(res.data);
print(res.realUri);
print(res.requestOptions);
print(' ');
if (res.statusCode == 200) {
connection.addAll(res.data);
}
} catch (e) {
print(e);
}
}
}
I need help, How can I get data by models to help me, please.
CodePudding user response:
Try this.
if (res.statusCode == 200) {
connection.addAll(res.data.map(connection) => Connection.fromJson(connection).toList());
}
.map produces a new list after transforming each element in a given list.
CodePudding user response:
Try this.
if (res.statusCode! >= 200 && res.statusCode! < 300) {
connection.addAll((res.data as List).map((e) => Connection.fromJson(e)).toList());
}
Don't forget assigning res.data to list. Hopefully it will work!!!
