Flutter(web)提供程序不在发布模式下工作,但在调试下工作

时间:2020-03-19 15:14:48

标签: dart provider flutter-web

我遇到了一些奇怪的行为,其中提供程序在调试模式与发布模式下的行为不同。

此问题发布在this question下。

这是功能的简短描述:我有一个ExpansionTiles列表(在其主体中包含TextFormFields)和一个FloatingActionButton,它添加到ExpansionTiles列表中。在这两个小部件下面,我有一个“后退”按钮和一个“下一个”按钮。只要ExpansionTiles列表中的任何TextFormField不完整,“下一步”按钮都必须为“不可用”(即,向用户抛出错误消息并显示为灰色)。所有TextFormFields准备就绪后,“下一步”按钮应更改颜色并将用户定向到新页面。更改颜色和功能可以在调试中正常运行,但不能在发布模式下运行(即在运行后:flutter build web)。

首先:TravelProceedModel监视用户是否可以单击“下一步”按钮的状态。就是这只是一个简单的布尔。每当调用updateCanProceed()时,我都会更新canProceed并同时通知notifyListeners()。

import 'package:flutter/material.dart';

class TravelProceedModel extends ChangeNotifier {

  bool canProceed;

  void updateCanProceed(bool value) {

    canProceed = value;

    notifyListeners();
  }
}

第二,使用ChangeNotifierProvider然后使用Consumer来实现TravelProceedModel。我期望(并且可以在调试中使用)更改canProceed时,以下行:“ nextEnabled:procedModel.canProceed”应该会使按钮更改颜色。但是,这仅在调试模式下有效。

class TravelDeductionTrips extends StatefulWidget {
  @override
  _TravelDeductionTripsState createState() => _TravelDeductionTripsState();
}

class _TravelDeductionTripsState extends State<TravelDeductionTrips> {

@override
Widget build(BuildContext context) {
  String year = Provider.of<UserSelectionsModel>.(context).selected_report["year"];
  String report = Provider.of<UserSelectionsModel>(context).selected_report["report"];

  return MultiProvider(
    providers: [
     ChangeNotifierProvider(
       create: (context) => TravelProceedModel()
     ),
     ChangeNotifierProvider(
       create: (context) => TravelModel(
         year: year,
         report: report,
       ),
     ),
    ],
    child: Consumer<TravelModel>(
      builder: (context, travelModel, child) {
       return FutureBuilder(
         future: travelModel.fetchTripsToLocalSession(),
         builder: (context, data) {
           if (data.connectionState == ConnectionState.done) {
             return Column(
               children: <Widget>[
                 TravelDeductionTripList(),
                 Consumer<TravelProceedModel>(
                   builder: (context, proceedModel, child) {
                     proceedModel.updateCanProceed(
                       travelModel.checkProceedCondition()
                     );
                     return BackNextButtons(
                       backEnabled: true,
                       nextEnabled: proceedModel.canProceed,
                       backText: "Tilbage",
                       nextText: travelModel.trips["next_action_title"],
                       errorMessage: travelModel.proceedErrorMessage,
                       nextFunction: () async {
                         await addQuestionIDtoStack(year, report, travelModel.trips["next_question_id"], true);
                       }, // No special function is needed, this is just a simple next
                     );
                   },
                 ),
               ],
             );
           } else {
             return Center(
               child: CircularProgressIndicator(),
             );
           }
         }
      );
    }
  ),
);
}
}

NB!从调用的TravelDeductionTripList()内部更新proceModel.canProceed

Provider.of<TravelProceedModel>(context, listen: false).updateCanProceed(
  Provider.of<TravelModel>(context, listen: false).checkProceedCondition()
);

“下一步”按钮仅在重新加载小部件后才更改颜色。就是看来TravelProceedModel正在更新,但未在UI中重绘该窗口小部件-尽管它在调试模式下发生的很好。

有人知道为什么调试有效但发布无效吗?

下面是我的“颤抖医生-v”。注意,这是一个混乱的Web项目。

[✓] Flutter (Channel beta, v1.15.17, on Mac OS X 10.15.2 19C57, locale en-US)
    • Flutter version 1.15.17 at /Users/danni/Documents/flutter/flutter
    • Framework revision 2294d75bfa (13 days ago), 2020-03-07 00:28:38 +0900
    • Engine revision 5aff311948
    • Dart version 2.8.0 (build 2.8.0-dev.12.0 9983424a3c)


[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
    • Android SDK at /Users/danni/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 installation is incomplete; a full installation is necessary for iOS development.
      Download at: https://developer.apple.com/xcode/download/
      Or install Xcode via the App Store.
      Once installed, run:
        sudo xcode-select --switch /Applications/Xcode.app/Contents/Developer
        sudo xcodebuild -runFirstLaunch
    ✗ CocoaPods installed but not working.
        You appear to have CocoaPods installed but it is not working.
        This can happen if the version of Ruby that CocoaPods was installed with is different from the one being used
        to invoke it.
        This can usually be fixed by re-installing CocoaPods. For more info, see
        https://github.com/flutter/flutter/issues/14293.
      To re-install CocoaPods, run:
        sudo gem install cocoapods

[✓] 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 41.0.2
    • Dart plugin version 191.8593
    • Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b49-5587405)

[✓] VS Code (version 1.43.0)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.8.1

[✓] Connected device (2 available)
    • Chrome     • chrome     • web-javascript • Google Chrome 80.0.3987.149
    • Web Server • web-server • web-javascript • Flutter Tools

! Doctor found issues in 1 category.

1 个答案:

答案 0 :(得分:-1)

我遇到了同样的问题,并且我已经解决了这个问题。

由于listen是一个参数,因此您可以向其传递任何其他bool变量(不应为常数true或false)。

foundation.dart包中有一个内置kReleaseMode的布尔变量。

这是一个示例,我如何处理调试和发布模式之间的此侦听器问题:

await Provider.of<AuthService>(context, listen: kReleaseMode).loginUser(
                  user: _username, password: _password);

对我来说,它运行完美。