-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson_reader.dart
36 lines (34 loc) · 1006 Bytes
/
json_reader.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import 'dart:convert';
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'package:http/http.dart' as http;
import 'package:portfolio/core/error/exceptions.dart';
Future<http.Response> readApi<T>(
String url, {
Map<String, String>? header,
}) async {
final subFolder = header != null && header['Language'] == 'fa' ? 'fa' : 'en';
return rootBundle.loadStructuredData<http.Response>(
'assets/api/$subFolder/$url.json',
(response) async {
try {
final encodedData = const Utf8Codec().encode(response);
final result = http.Response.bytes(
encodedData,
200,
headers: header ?? {},
request: http.Request(
'GET',
Uri.parse(
'assets/api/$subFolder/$url.json',
),
),
);
debugPrint(result.body);
return result;
} catch (error) {
throw ServerException(message: error.toString());
}
},
);
}