Flutter - 格式异常:输入意外结束(在字符 1 处)

Flutter - Format Exception : Unexpected end of input (at character 1)

我在从 .json 文件加载 json 时遇到此异常。完整的例外是:

E/flutter ( 4062): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: 
FormatException: Unexpected end of input (at character 1)
E/flutter ( 4062): 
E/flutter ( 4062): ^
E/flutter ( 4062): 
E/flutter ( 4062): #0      _ChunkedJsonParser.fail (dart:convert-  patch/convert_patch.dart:1405:5)
E/flutter ( 4062): #1      _ChunkedJsonParser.close (dart:convert-patch/convert_patch.dart:523:7)
E/flutter ( 4062): #2      _parseJson (dart:convert-patch/convert_patch.dart:41:10)
E/flutter ( 4062): #3      JsonDecoder.convert (dart:convert/json.dart:506:36)
E/flutter ( 4062): #4      JsonCodec.decode (dart:convert/json.dart:157:41)
E/flutter ( 4062): #5      _HomePageState.loadJsonData.<anonymous closure>.<anonymous closure> (package:audio_player/presentation/pages/home_page.dart:22:35)
E/flutter ( 4062): #6      State.setState (package:flutter/src/widgets/framework.dart:1088:30)
E/flutter ( 4062): #7      _HomePageState.loadJsonData.<anonymous closure> (package:audio_player/presentation/pages/home_page.dart:20:27)
E/flutter ( 4062): #8      _rootRunUnary (dart:async/zone.dart:1436:47)
E/flutter ( 4062): #9      _CustomZone.runUnary (dart:async/zone.dart:1335:19)
E/flutter ( 4062): <asynchronous suspension>
E/flutter ( 4062): #10     _HomePageState.loadJsonData (package:audio_player/presentation/pages /home_page.dart:19:5)
E/flutter ( 4062): <asynchronous suspension>

我的 Json 文件路径 (lib/utils/json/popular_books.json) 和代码如下所示:

[
  {
   "rating":"4.5",
   "title":"sometitle",
   "text":"sometext",
   "img":"lib/utils/assets/image_1",
  }
   ...
]

我的图像在同一个文件夹中 utils/assets。我正在加载 json

  List? popularBooks;

  loadJsonData() async {
    await DefaultAssetBundle.of(context).loadString("lib/utils/json/popular_books.json").then(
          (loadedJson) => setState(
            () {
              popularBooks = json.decode(loadedJson);
            },
          ),
        );
      }

  @override
  void initState() {
  loadJsonData();
  super.initState();
 }

我正在尝试仅加载页面视图中的图像:

PageView.builder(
              itemCount: popularBooks==null?0:popularBooks!.length,
              controller: _controller,
              itemBuilder: (context, index) {
                return Container(
                  margin: EdgeInsets.only(right: 10),
                  height: 150,
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(10),
                   ),
                   child: Image.asset("${popularBooks![index]["img"]}"),
                 );
               },
             ),

然后是这个格式异常如果您有任何修复请分享我非常感谢。 <3

您的JSON无效。使用 json 验证器,您可以发现问题。在您的 JSON 中有以下语法错误:

[
  {
   "rating":"4.5",
   "title":"sometitle",
   "text":"sometext",
   "img":"lib/utils/assets/image_1", <-- This comma is the problem
  }
]