我遇到了一些奇怪的行为,提供程序在调试模式和发布模式下的行为不同。
这个问题发布在这个问题的后面。
以下是功能的简短描述:我有一个ExpansionTiles列表(在其主体中包含TextFormFields(和一个FloatingActionButton,它添加到ExpansionTiles列表中。在这两个小部件下面,我有一个Back按钮和一个Next按钮。只要ExpansionTiles列表中的任何TextFormField都不完整,"下一步"按钮就需要"不可用"(即向用户抛出错误消息并将其颜色设为灰色(。一旦所有的TextFormFields都准备好了,"下一步"按钮就应该更改颜色并将用户引导到新页面。更改颜色和功能在调试中可以很好地工作,但在发布模式中不行(即运行后:flutter build-web(。
首先:TravelProceedModel监控用户是否可以单击"下一步"按钮的状态。这只是一个简单的废话。每当调用updateCanProceed((时,我都会更新canProceed并通知Listeners((。
import 'package:flutter/material.dart';
class TravelProceedModel extends ChangeNotifier {
bool canProceed;
void updateCanProceed(bool value) {
canProceed = value;
notifyListeners();
}
}
其次,TravelProceedModel是使用ChangeNotificationProvider和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(),
);
}
}
);
}
),
);
}
}
NB!processedModel.canProcessed从调用的TravelDepression TripList((中更新
Provider.of<TravelProceedModel>(context, listen: false).updateCanProceed(
Provider.of<TravelModel>(context, listen: false).checkProceedCondition()
);
"下一步"按钮仅在重新加载小部件后更改颜色。也就是说,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.
我遇到了同样的问题,我已经找到了解决方法。
由于listen
是一个参数,您可以将任何其他布尔变量传递给它(不应该是常量true或false(。
foundation.dart
包中有一个内置的bool变量,名为kReleaseMode。
这里有一个例子,我是如何在调试和发布模式之间处理这个监听器问题的:
await Provider.of<AuthService>(context, listen: kReleaseMode).loginUser(
user: _username, password: _password);
对我来说,它非常有效。