· 4 years ago · May 21, 2021, 08:02 AM
1I/flutter (16982): List<dynamic>
2I/flutter (16982): type 'List<dynamic>' is not a subtype of type 'Map<String, dynamic>'
3Reloaded 6 of 748 libraries in 1,465ms.
4I/flutter (16982): api get recieved!
5I/flutter (16982): ******************************************************************
6I/flutter (16982): List<dynamic>
7I/flutter (16982): type 'List<dynamic>' is not a subtype of type 'Map<String, dynamic>'
8Reloaded 6 of 748 libraries in 1,456ms.
9I/flutter (16982): api get recieved!
10I/flutter (16982): ******************************************************************
11I/flutter (16982): List<dynamic>
12I/flutter (16982): type 'List<dynamic>' is not a subtype of type 'Map<String, dynamic>'
13Reloaded 6 of 748 libraries in 1,469ms.
14Reloaded 7 of 748 libraries in 1,491ms.
15Reloaded 6 of 748 libraries in 1,538ms.
16I/flutter (16982): api get recieved!
17I/flutter (16982): type 'List<dynamic>' is not a subtype of type 'Map<String, dynamic>' in type cast
18Reloaded 6 of 748 libraries in 1,404ms.
19
20
21==============================================================================================================
22
23
24import 'dart:io';
25import 'package:battle_line/constant/api_route.dart';
26import 'package:battle_line/services/http_exceptions.dart';
27import 'package:http/http.dart' as http;
28import 'dart:convert';
29import 'dart:async';
30
31class ApiBaseHelper {
32 Future get(String url) async {
33 var responseJson;
34 // print(Uri.http(ApiRoute.baseApi, url));
35 try {
36 final response = await http.get(Uri.http(ApiRoute.baseApi, url));
37 responseJson = _returnResponse(response);
38 } on SocketException {
39 print('No net');
40 throw FetchDataException('No Internet connection');
41 }
42 print('api get recieved!');
43 return responseJson;
44 }
45}
46
47dynamic _returnResponse(http.Response response) {
48 switch (response.statusCode) {
49 case 200:
50 var responseJson = json.decode(response.body.toString());
51 // print(response.body);
52 return responseJson;
53 case 400:
54 throw BadRequestException(response.body.toString());
55 case 401:
56 case 403:
57 throw UnauthorisedException(response.body.toString());
58 case 500:
59 default:
60 throw FetchDataException('Error occured while Communication with Server with StatusCode : ${response.statusCode}');
61 }
62}
63
64
65==============================================================================================================================
66
67import 'package:battle_line/constant/api_route.dart';
68import 'package:battle_line/model/game_banner_model.dart';
69import 'package:battle_line/services/base_api.dart';
70
71class BannerRepository {
72 ApiBaseHelper _helper = ApiBaseHelper();
73
74 Future<List<GameBannerModel>> fetchBannerList() async {
75 final response = await _helper.get(ApiRoute.gameBannerApi);
76 print("******************************************************************");
77 print(response.runtimeType);
78 print(BannerResponse.fromJson(response).results);
79 print((BannerResponse.fromJson(response).results).runtimeType);
80 print("******************************************************************");
81 return BannerResponse.fromJson(response).results;
82 }
83}
84
85
86==============================================================================================================================
87
88
89class GameBannerModel {
90 int id;
91 int gameId;
92 String gameName;
93 String gameSlug;
94 String gamePlateform;
95 String image;
96 String sImage;
97 String mImage;
98 String tImage;
99
100 GameBannerModel.formJson(Map<String, dynamic> json) {
101 id = json["id"];
102 gameId = json["game_category"]["id"];
103 gameName = json["game_category"]["name"];
104 gameSlug = json["game_category"]["slug"];
105 gamePlateform = json["game_category"]["plateform"];
106 image = json["image"]["url"];
107 sImage = json["image"]["formats"]["small"]["url"];
108 mImage = json["image"]["formats"]["medium"]["url"];
109 tImage = json["image"]["formats"]["thumbnail"]["url"];
110 }
111
112 Map<String, dynamic> toJson() {
113 final Map<String, dynamic> data = new Map<String, dynamic>();
114 data["id"] = this.id;
115 data["game_category"]["id"] = this.gameId;
116 data["game_category"]["name"] = this.gameName;
117 data["game_category"]["slug"] = this.gameSlug;
118 data["game_category"]["plateform"] = this.gamePlateform;
119 data["image"]["url"] = this.image;
120 data["image"]["formats"]["small"]["url"] = this.sImage;
121 data["image"]["formats"]["medium"]["url"] = this.mImage;
122 data["image"]["formats"]["thumbnail"]["url"] = this.tImage;
123 return data;
124 }
125}
126
127class BannerResponse {
128 int page;
129 int totalResults;
130 int totalPages;
131 List<GameBannerModel> results;
132
133 BannerResponse({this.page, this.totalResults, this.totalPages, this.results});
134
135 BannerResponse.fromJson(Map<String, dynamic> json) {
136 page = json['page'];
137 totalResults = json['total_results'];
138 totalPages = json['total_pages'];
139 if (json['results'] != null) {
140 results = <GameBannerModel>[];
141 json['results'].forEach((v) {
142 results.add(new GameBannerModel.formJson(json));
143 });
144 }
145 }
146
147 Map<String, dynamic> toJson() {
148 final Map<String, dynamic> data = Map<String, dynamic>();
149 data['page'] = this.page;
150 data['total_results'] = this.totalResults;
151 data['total_pages'] = this.totalPages;
152 if (this.results != null) {
153 data['results'] = this.results.map((v) => v.toJson()).toList();
154 }
155 return data;
156 }
157}
158
159===========================================================================================================================
160
161
162
163
164import 'package:battle_line/bloc/banner_block.dart';
165import 'package:battle_line/constant/colors.dart';
166import 'package:battle_line/constant/dimensions.dart';
167import 'package:battle_line/constant/textstyle.dart';
168import 'package:battle_line/model/game_banner_model.dart';
169import 'package:battle_line/services/api_responce.dart';
170import 'package:battle_line/view/screens/loading.dart';
171import 'package:battle_line/view/widget/app_bar.dart';
172import 'package:battle_line/view/screens/error/error_message.dart';
173import 'package:carousel_slider/carousel_slider.dart';
174import 'package:flutter/material.dart';
175
176class MyHomeScreen extends StatefulWidget {
177 const MyHomeScreen({Key key}) : super(key: key);
178
179 @override
180 _MyHomeScreenState createState() => _MyHomeScreenState();
181}
182
183class _MyHomeScreenState extends State<MyHomeScreen> {
184 BannerBloc _bannerBloc;
185
186 @override
187 void initState() {
188 super.initState();
189 _bannerBloc = BannerBloc();
190 }
191
192 @override
193 void dispose() {
194 _bannerBloc.dispose();
195 super.dispose();
196 }
197
198 @override
199 Widget build(BuildContext context) {
200 // MyTextStyle _textStyle = MyTextStyle();
201 return SafeArea(
202 child: Scaffold(
203 appBar: PreferredSize(
204 preferredSize: Size.fromHeight(kAppBarHeight),
205 child: MyAppBarWidet(textTitle1: "Battle", textTitle2: 'Line'),
206 ),
207 body: RefreshIndicator(
208 onRefresh: () => _bannerBloc.fetchBannerList(),
209 child: StreamBuilder<ApiResponse<List<GameBannerModel>>>(
210 stream: _bannerBloc.bannerListStream,
211 builder: (context, snapshot) {
212 if (snapshot.hasData) {
213 switch (snapshot.data.status) {
214 case Status.COMPLETED:
215 return HomeScreen();
216 break;
217 case Status.LOADING:
218 return Loading(loadingMessage: snapshot.data.message);
219 break;
220 case Status.ERROR:
221 return Error(
222 errorMessage: snapshot.data.message,
223 onRetryPressed: () => _bannerBloc.fetchBannerList(),
224 );
225 break;
226 default:
227 return Container(
228 child: Center(
229 child: Text("nothing"),
230 ),
231 );
232 break;
233 }
234 }
235 return Container(
236 child: Center(
237 child: Text("data"),
238 ),
239 );
240 },
241 ),
242 ),
243 ),
244 );
245 }
246}
247
248class HomeScreen extends StatefulWidget {
249 const HomeScreen({
250 Key key,
251 }) : super(key: key);
252
253 @override
254 _HomeScreenState createState() => _HomeScreenState();
255}
256
257class _HomeScreenState extends State<HomeScreen> {
258 int _currentIndex = 0;
259 @override
260 Widget build(BuildContext context) {
261 MyTextStyle _textStyle = MyTextStyle();
262 return Container(
263 padding: EdgeInsets.all(8),
264 child: Column(
265 crossAxisAlignment: CrossAxisAlignment.start,
266 children: [
267 ..............................
268 )
269 ],
270 ),
271 );
272 }
273}
274
275