简体   繁体   中英

Assertion fails on Web version, but succeeds on iOS Flutter

I have a different behaviour when I run my app on Chrome then when I run it on iOS. It happens when I get to this screen:

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(),
          ),
        ),
      ],
    );
  }
}

If on iOS the screen loads normally, while when running the web version it throws the Another exception was thrown: Failed assertion: boolean expression must not be null error. Do you now what could cause this different behaviour? I followed all the widgets that pass the userRepository along the tree and is all good..as in fact on iOS runs properly.

Here is my flutter doctor:

[✓] 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!

Do you see anything to update? I'm on High Sierra so I think I can't go above Flutter version 1.12.13+hotfix.5. Also I see that the channel is unknown..could it have something to do with it? As always many thanks for your time and help. Cheers.

found the reason.. it wasn't the assertion but the platform check I was doing on backButton , that only included iOS..I added the check for the web and it now works es expected. I'll leave the question up as might help others porting their apps to web.

dynamic backButton;
    if (kIsWeb) {
      backButton = Icons.arrow_back;
    } else if (Platform.isIOS) {
      backButton = CupertinoIcons.back;
    } else {
      backButton = Icons.arrow_back;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM