Flutter(Web)提供程序不在发布模式下工作,但在调试中工作
Posted
技术标签:
【中文标题】Flutter(Web)提供程序不在发布模式下工作,但在调试中工作【英文标题】:Flutter (web) Provider not working in released mode, but working in debug 【发布时间】:2020-06-30 18:10:42 【问题描述】:我遇到了一些奇怪的行为,即 Provider 在调试和发布模式下的行为不同。
这个问题发在offthis question。
这里是功能的简短描述:我有一个 ExpansionTiles 列表(在它们的主体中包含 TextFormFields)和一个添加到 ExpansionTiles 列表的 FloatingActionButton。在这两个小部件下方,我有一个返回按钮和一个下一步按钮。只要 ExpansionTiles 列表中的任何 TextFormField 不完整,Next 按钮就必须是“不可用的”(即向用户抛出错误消息并显示为灰色)。一旦所有 TextFormFields 准备就绪,Next 按钮应该会改变颜色并将用户引导到新页面。更改颜色和功能在调试中可以正常工作,但在发布模式下则不行(即运行后:flutter build web)。
首先:TravelProceedModel 监控用户是否可以点击下一步按钮的状态。 IE。这只是一个简单的布尔值。每当调用 updateCanProceed() 时,我都会更新 canProceed 和 notifyListeners()。
import 'package:flutter/material.dart';
class TravelProceedModel extends ChangeNotifier
bool canProceed;
void updateCanProceed(bool value)
canProceed = value;
notifyListeners();
其次,TravelProceedModel 使用 ChangeNotifierProvider 实现,然后使用 Consumer。我期待(并且这在调试中有效)当 canProceed 更改此行时:“nextEnabled:proceedModel.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(),
);
);
),
);
注意! proceedModel.canProceed 从调用的 TravelDeductionTripList() 中更新
Provider.of<TravelProceedModel>(context, listen: false).updateCanProceed(
Provider.of<TravelModel>(context, listen: false).checkProceedCondition()
);
“下一步”按钮仅在重新加载小部件后更改颜色。 IE。似乎 TravelProceedModel 正在更新,但小部件并未在 UI 中重绘 - 尽管它在调试模式下完美发生。
有人知道为什么调试有效但发布无效吗?
下面是我的“颤振医生-v”。请注意,这是一个 Flutter 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】:我遇到了同样的问题,我已经解决了这个问题。
由于listen
是一个参数,您可以将任何其他bool 变量传递给它(不应是一个常量true 或false)。
foundation.dart
包中有一个内置的 bool 变量,名为 kReleaseMode。
所以这里是一个例子,我是如何在调试和发布模式之间处理这个监听器问题的:
await Provider.of<AuthService>(context, listen: kReleaseMode).loginUser(
user: _username, password: _password);
对我来说,它完美无缺。
【讨论】:
我有同样的问题,我尝试了这个解决方案,但对我来说不起作用。以上是关于Flutter(Web)提供程序不在发布模式下工作,但在调试中工作的主要内容,如果未能解决你的问题,请参考以下文章
Flutter Material Icons (for web) 在发布模式下不起作用