如何将字符串转换为flutter中的列表?

时间:2020-05-09 22:59:24

标签: flutter dart

我有一个API,该API可以按如下方式调用json String数组:

[concoxgt06, 0, 0, 0, JH 02AF 8525, img/markers/objects/land-truck.svg, {arrow_no_connection: arrow_red, arrow_stopped: arrow_red, arrow_moving: arrow_green, arrow_engine_idle: off}, arrow, #00FF44, 7, , 9297894107, 0@0, 7004924982,kumar, JH 02AF 8525, gps, acc, 28728, 3036, {source: rates, measurement: l100km, cost: 0, summer: 0, winter: 0, winter_start: 12-01, winter_end: 03-01}, , {stops: gps, min_moving_speed: 6, min_idle_speed: 3, min_diff_points: 0.0005, use_gpslev: false, min_gpslev: 5, use_hdop: false, max_hdop: 3, min_fuel_speed: 10, min_ff: 10, min_ft: 10}, {484: {name: Ignition, type: acc, param: acc, data_list: true, popup: true, result_type: logic, text_1: On, text_0: Off, units: , lv: 0, hv: 0, acc_ignore: false, formula: , calibration: [], dictionary: []}}, [], [], [pump, track, bats, acc, batl, gpslev, defense], true, true, 2020-06-11]

我打算将json转换为飞镖中的List。我尝试了以下脚本:

json.decode(response.body).cast<List<String>();

List<String> stringList = (jsonDecode(input) as List<dynamic>).cast<String>();

但是它不起作用,脚本应该如何正确?

这是我收到的错误

E/flutter (22486): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: FormatException: Unexpected character (at character 2)
E/flutter (22486): [digitalsystemsdsf, 0, 0, 0, Bolero 2275, data/user/objects/2_b10518926dda5...
E/flutter (22486):  ^
E/flutter (22486): 
E/flutter (22486): #0      _ChunkedJsonParser.fail (dart:convert-patch/convert_patch.dart:1394:5)
E/flutter (22486): #1      _ChunkedJsonParser.parseNumber (dart:convert-patch/convert_patch.dart:1261:9)
E/flutter (22486): #2      _ChunkedJsonParser.parse (dart:convert-patch/convert_patch.dart:926:22)
E/flutter (22486): #3      _parseJson (dart:convert-patch/convert_patch.dart:31:10)
E/flutter (22486): #4      JsonDecoder.convert (dart:convert/json.dart:505:36)
E/flutter (22486): #5      JsonCodec.decode (dart:convert/json.dart:153:41)
E/flutter (22486): #6      jsonDecode (dart:convert/json.dart:96:10)
E/flutter (22486): #7      _HomeScreenContainerState.fetchFnSettings (package:speedotrack/component/component_home_screen.dart:111:13)
E/flutter (22486): <asynchronous suspension>
E/flutter (22486): #8      _HomeScreenContainerState.homeScreenMethods (package:speedotrack/component/component_home_screen.dart:89:5)
E/flutter (22486): #9      _rootRunUnary (dart:async/zone.dart:1192:38)
E/flutter (22486): #10     _CustomZone.runUnary (dart:async/zone.dart:1085:19)
E/flutter (22486): #11     _FutureListener.handleValue (dart:async/future_impl.dart:141:18)
E/flutter (22486): #12     Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:682:45)
E/flutter (22486): #13     Future._propagateToListeners (dart:async/future_impl.dart:711:32)
E/flutter (22486): #14     Future._completeWithValue (dart:async/future_impl.dart:526:5)
E/flutter (22486): #15     _AsyncAwaitCompleter.complete (dart:async-patch/async_patch.dart:36:15)
E/flutter (22486): #16     _completeOnAsyncReturn (dart:async-patch/async_patch.dart:298:13)
E/flutter (22486): #17     MainPrefs.setMainPrefs (package:speedotrack/sharedPrefs/main_prefs.dart)
E/flutter (22486): #18     _rootRunUnary (dart:async/zone.dart:1192:38)
E/flutter (22486): #19     _CustomZone.runUnary (dart:async/zone.dart:1085:19)
E/flutter (22486): #20     _FutureListener.handleValue (dart:async/future_impl.dart:141:18)
E/flutter (22486): #21     Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:682:45)
E/flutter (22486): #22     Future._propagateToListeners (dart:async/future_impl.dart:711:32)
E/flutter (22486): #23     Future._addListener.<anonymous closure> (dart:async/future_impl.dart:391:9)
E/flutter (22486): #24     _rootRun (dart:async/zone.dart:1184:13)
E/flutter (22486): #25     _CustomZone.run (dart:async/zone.dart:1077:19)
E/flutter (22486): #26     _CustomZone.runGuarded (dart:async/zone.dart:979:7)
E/flutter (22486): #27     _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:1019:23)
E/flutter (22486): #28     _microtaskLoop (dart:async/schedule_microtask.dart:43:21)
E/flutter (22486): #29     _startMicrotaskLoop (dart:async/schedule_microtask.dart:52:5)

1 个答案:

答案 0 :(得分:0)

如果要转换以“,”分隔的字符串以列出,可以通过以下方式进行:

String myString = "foo,bar,foobar";

List<String> stringList = myString.split(",");

但是在您的情况下,它无法完成任务。您应该做的就是将json转换为对象。我建议谷歌搜索json反序列化。