断言在 Web 版本上失败,但在 iOS Flutter 上成功
Posted
技术标签:
【中文标题】断言在 Web 版本上失败,但在 iOS Flutter 上成功【英文标题】:Assertion fails on Web version, but succeeds on iOS Flutter 【发布时间】:2020-08-20 07:03:12 【问题描述】:当我在 Chrome 上运行我的应用程序与在 ios 上运行它时,我的行为有所不同。当我到达此屏幕时会发生这种情况:
import 'dart:io';
import 'package:fixit_shop_flutter/fixit_shop_app/authentication_bloc/user_repository.dart';
import 'package:fixit_shop_flutter/fixit_shop_app/register/bloc/register_bloc.dart';
import 'package:fixit_shop_flutter/fixit_shop_app/register/register_form.dart';
import 'package:fixit_shop_flutter/localization.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
class RegisterScreen extends StatelessWidget
final UserRepository _userRepository;
RegisterScreen(Key key, @required UserRepository userRepository)
: assert(userRepository != null),
_userRepository = userRepository,
super(key: key);
@override
Widget build(BuildContext context)
dynamic backButton =
Platform.isIOS ? CupertinoIcons.back : Icons.arrow_back;
return Stack(
children: [
Image.asset('assets/mainBg.png',
height: MediaQuery.of(context).size.height,
width: MediaQuery.of(context).size.width,
fit: BoxFit.cover),
Scaffold(
backgroundColor: Colors.transparent,
appBar: AppBar(
centerTitle: true,
elevation: 0,
title: Text(
AppLocalizations.instance.text('RegisterScreenTitle'),
// textAlign: TextAlign.center,
style: TextStyle(
color: Colors.orange,
fontSize: 22,
fontWeight: FontWeight.w500,
letterSpacing: 1),
),
backgroundColor: Colors.transparent,
leading: IconButton(
icon: Icon(backButton),
color: Colors.redAccent,
onPressed: ()
Navigator.pop(context);
),
),
body: BlocProvider<RegisterBloc>(
create: (context) => RegisterBloc(userRepository: _userRepository),
child: RegisterForm(),
),
),
],
);
如果在 iOS 上屏幕加载正常,而在运行 web 版本时会抛出 Another exception was thrown: Failed assertion: boolean expression must not be null
错误。
你现在有什么可能导致这种不同的行为吗?我跟踪了沿树传递 userRepository 的所有小部件,一切都很好..实际上在 iOS 上运行正常。
这是我的颤振医生:
[✓] Flutter (Channel unknown, v1.12.13+hotfix.5, on Mac OS X 10.13.6 17G65, locale en-IT)
• Flutter version 1.12.13+hotfix.5 at /Users/vinnytwice/Developer/flutter
• Framework revision 27321ebbad (5 months ago), 2019-12-10 18:15:01 -0800
• Engine revision 2994f7e1e6
• Dart version 2.7.0
[✓] android toolchain - develop for Android devices (Android SDK version 29.0.2)
• Android SDK at /Users/vinnytwice/Library/Android/sdk
• Android NDK location not configured (optional; useful for native profiling support)
• Platform android-29, build-tools 29.0.2
• Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
• Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b49-5587405)
• All Android licenses accepted.
[✓] Xcode - develop for iOS and macOS (Xcode 10.2.1)
• Xcode at /Applications/Xcode.app/Contents/Developer
• Xcode 10.2.1, Build version 10E1001
• CocoaPods version 1.8.1
[✓] Chrome - develop for the web
• Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
[✓] Android Studio (version 3.5)
• Android Studio at /Applications/Android Studio.app/Contents
• Flutter plugin version 44.0.1
• Dart plugin version 191.8593
• Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b49-5587405)
[✓] Connected device (3 available)
• iPad Pro (10.5-inch) • 23C29147-A4F3-4B9F-8182-9C813D5A54AD • ios • com.apple.CoreSimulator.SimRuntime.iOS-12-2 (simulator)
• Chrome • chrome • web-javascript • Google Chrome 81.0.4044.129
• Web Server • web-server • web-javascript • Flutter Tools
• No issues found!
你有什么要更新的吗?我在 High Sierra,所以我认为我不能超过 Flutter 版本 1.12.13+hotfix.5。我也看到频道是未知的..它与它有关吗? 一如既往地感谢您的时间和帮助。 干杯。
【问题讨论】:
【参考方案1】:找到原因.. 不是断言,而是我在backButton
上进行的平台检查,仅包括 iOS..我添加了对网络的检查,它现在可以正常工作。我会留下这个问题,因为这可能有助于其他人将他们的应用程序移植到网络上。
dynamic backButton;
if (kIsWeb)
backButton = Icons.arrow_back;
else if (Platform.isIOS)
backButton = CupertinoIcons.back;
else
backButton = Icons.arrow_back;
【讨论】:
以上是关于断言在 Web 版本上失败,但在 iOS Flutter 上成功的主要内容,如果未能解决你的问题,请参考以下文章
Mongo Shell 显示添加的文档,但在 Scalatest 中断言失败
CBPeripheralManager AddService 上的 iOS 崩溃断言失败:
Axios GET请求在我的本地版本上运行,但在Heroku上失败-未捕获(承诺)错误:请求失败,状态码为500