即使在默认的默认 Flutter Web 项目中也出现错误“FormatException:意外字符(在字符 1)”

Posted

技术标签:

【中文标题】即使在默认的默认 Flutter Web 项目中也出现错误“FormatException:意外字符(在字符 1)”【英文标题】:Error "FormatException: Unexpected character (at character 1)" even in default default flutter web project 【发布时间】:2019-12-19 18:38:47 【问题描述】:

每当我创建一个新的 Flutter Web 项目时,都会出现同样的错误:Unexpected Character (at character 1)

当我运行webdev serve 时,它会出现一个空白屏幕并停止。 并且,在 webdev 守护进程上我得到了错误。

我没有对代码做任何改动,照原样执行,也就是默认的flutter web项目。

我已经卸载并重新安装,尝试过flutter clean,但这些都不起作用:

更多详情如下:

Unhandled exception:
FormatException: Unexpected character (at character 1)
Not Found
^

#0      _ChunkedJsonParser.fail (dart:convert-patch/convert_patch.dart:1392:5)
#1      _ChunkedJsonParser.parseNumber (dart:convert-patch/convert_patch.dart:1259:9)
#2      _ChunkedJsonParser.parse (dart:convert-patch/convert_patch.dart:924:22)
#3      _parseJson (dart:convert-patch/convert_patch.dart:29:10)
#4      JsonDecoder.convert (dart:convert/json.dart:493:36)
#5      JsonCodec.decode (dart:convert/json.dart:151:41)
#6      jsonDecode (dart:convert/json.dart:94:10)
#7      parse (package:source_maps/parser.dart:27:15)
#8      Sources.scriptParsed (package:dwds/src/debugging/sources.dart:62:19)
<asynchronous suspension>
#9      _RootZone.runUnaryGuarded (dart:async/zone.dart:1314:10)
#10     CastStreamSubscription._onData (dart:_internal/async_cast.dart:81:11)
#11     _RootZone.runUnaryGuarded (dart:async/zone.dart:1314:10)
#12     _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:336:11)
#13     _BufferingStreamSubscription._add (dart:async/stream_impl.dart:263:7)
#14     _SinkTransformerStreamSubscription._add (dart:async/stream_transformers.dart:68:11)
#15     _EventSinkWrapper.add (dart:async/stream_transformers.dart:15:11)
#16     WipDomain.eventStream.<anonymous closure>.<anonymous closure> (package:webkit_inspection_protocol/webkit_inspection_protocol.dart:301:26)
#17     _HandlerEventSink.add (dart:async/stream_transformers.dart:228:7)
#18     _SinkTransformerStreamSubscription._handleData (dart:async/stream_transformers.dart:120:24)
#19     _RootZone.runUnaryGuarded (dart:async/zone.dart:1314:10)
#20     _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:336:11)
#21     _DelayedData.perform (dart:async/stream_impl.dart:591:14)
#22     _StreamImplEvents.handleNext (dart:async/stream_impl.dart:707:11)
#23     _PendingEvents.schedule.<anonymous closure> (dart:async/stream_impl.dart:667:7)
#24     _microtaskLoop (dart:async/schedule_microtask.dart:41:21)
#25     _startMicrotaskLoop (dart:async/schedule_microtask.dart:50:5)
#26     _runPendingImmediateCallback (dart:isolate-patch/isolate_patch.dart:116:13)
#27     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:173:5)
pub failed (255)

我的 main.dart

import 'package:flutter_web/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget 
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) 
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  


class MyHomePage extends StatelessWidget 
  MyHomePage(Key key, this.title) : super(key: key);

  final String title;

  @override
  Widget build(BuildContext context) 
    // The Flutter framework has been optimized to make rerunning build methods
    // fast, so that you can just rebuild anything that needs updating rather
    // than having to individually change instances of widgets.
    return Scaffold(
      appBar: AppBar(
        title: Text(title),
      ),
      body: Center(
        // Center is a layout widget. It takes a single child and positions it
        // in the middle of the parent.
        child: Column(
          // Column is also layout widget. It takes a list of children and
          // arranges them vertically. By default, it sizes itself to fit its
          // children horizontally, and tries to be as tall as its parent.
          //
          // Invoke "debug painting" (choose the "Toggle Debug Paint" action
          // from the Flutter Inspector in android Studio, or the "Toggle Debug
          // Paint" command in Visual Studio Code) to see the wireframe for each
          // widget.
          //
          // Column has various properties to control how it sizes itself and
          // how it positions its children. Here we use mainAxisAlignment to
          // center the children vertically; the main axis here is the vertical
          // axis because Columns are vertical (the cross axis would be
          // horizontal).
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'Hello, World!',
            ),
          ],
        ),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  


https://github.com/ipshitag/hello_world/tree/master/helloworld

【问题讨论】:

我不知道 web 的颤振,但看起来有些代码正在尝试解析一些 json,但它收到的文本是“未找到”。 但我没有添加任何 json 文件。如果默认有的话,它在哪里?我想不通 能否请您发布您的项目或一些示例代码? 它是默认的颤振项目。但是,我仍在添加我的 main.dart,您可以在 github.com/ipshitag/hello_world/tree/master/helloworld 看到所有文件 【参考方案1】:

这是 Windows 上的一个 webdev 错误,已在 webdev v2.5.0 中修复:

https://github.com/dart-lang/webdev/issues/514 https://github.com/dart-lang/webdev/pull/563

运行pub global activate webdev 以更新到最新版本。

【讨论】:

以上是关于即使在默认的默认 Flutter Web 项目中也出现错误“FormatException:意外字符(在字符 1)”的主要内容,如果未能解决你的问题,请参考以下文章

如何更改 Flutter Web 应用的默认 Web 服务器 ip(127.0.0.1)

flutter web开发环境配置

flutter web开发环境配置

即使选择了默认的苹果 llvm 5.0,XCode5 也使用 gcc48

如何构造 FCM 推送通知以在 Flutter(Android 和 iOS)的默认系统 Web 浏览器应用中打开指定的 URL

如何使用自定义端口运行 Flutter Web