这段代码中有一些非常根本性的错误,请看看这个。我也是新来的,所以建议我正确的路径来继续
我试图从类'TurnOnSwitchAndSlider'到类'BatteryIsChargedAsPerYourRequirement'访问这个变量'light',由于某种原因,我得到了ProviderNotFoundException (Error: Could not find the correct Provider<AppState> above this TurnOnSwitchAndSlider Widget
turn_on_switch.dart
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
class AppState extends ChangeNotifier {
bool light = true;
double currValueForSlider = 85;
void updateLight(bool value){
light = value;
notifyListeners();
}
void updateSlider(double value){
currValueForSlider = value;
notifyListeners();
}
}
class TurnOnSwitchAndSlider extends StatefulWidget {
const TurnOnSwitchAndSlider({super.key});
@override
State<TurnOnSwitchAndSlider> createState() => _TurnOnSwitchAndSliderState();
}
class _TurnOnSwitchAndSliderState extends State<TurnOnSwitchAndSlider> {
@override
Widget build(BuildContext context) {
return Column(
children: [
Switch(
value: context.watch<AppState>().light,
onChanged: (bool value){
context.read<AppState>().updateLight(value);
},
),
Slider(
value: context.watch<AppState>().currValueForSlider,
max: 100,
divisions: 20,
label: context.watch<AppState>().currValueForSlider.round().toString(),
onChanged: (double value) {
context.read<AppState>().updateSlider(value);
},
)
],
);
}
}
字符串
battery_is_charge_as_per_your_requirement.dart
import 'dart:async';
import 'package:battery_plus/battery_plus.dart';
import 'package:flutter/material.dart';
import 'package:my_battery_lives/widgets/turn_on_switch.dart';
import 'package:provider/provider.dart';
class BatteryIsChargedAsPerYourRequirement extends StatefulWidget {
const BatteryIsChargedAsPerYourRequirement({super.key});
@override
State<BatteryIsChargedAsPerYourRequirement> createState() => _BatteryIsChargedAsPerYourRequirementState();
}
class _BatteryIsChargedAsPerYourRequirementState extends State<BatteryIsChargedAsPerYourRequirement> {
final Battery _battery = Battery();
late Timer timer;
int intBatteryLevel=0;
String isChargedOrNot = '';
int setBatteryPercentage = 85;
bool boolShowAlert = true;
BatteryState? batteryState;
StreamSubscription <BatteryState>? batteryStateSubscription;
Future<void> getBatteryLevel() async{
final batteryLevel = await _battery.batteryLevel;
setState(() {
intBatteryLevel = batteryLevel;
});
}
void updateBatteryState(BatteryState state){
if(batteryState == state) return;
setState(() {
batteryState = state;
});
}
void _showAlert(BuildContext buildContext){
showDialog(context: context, builder: (context){
return AlertDialog(
title: const Text('Remove the Charger!'),
content: Text('Battery is now over $setBatteryPercentage, please remove the charger!'),
actions: <Widget>[
TextButton(onPressed: (){
Navigator.of(context).pop();
}, child: const Text('Cancel!'))
],
);
});
}
@override
void initState() {
super.initState();
timer = Timer.periodic(const Duration(seconds: 1), (timer) {
getBatteryLevel();
_battery.batteryState.then(updateBatteryState);
batteryStateSubscription = _battery.onBatteryStateChanged.listen(updateBatteryState);
bool light = context.watch<AppState>().light;
if('$batteryState' == 'BatteryState.discharging' && intBatteryLevel < 40){
isChargedOrNot = 'Connect the charger';
boolShowAlert = true;
}else if(intBatteryLevel == 100){
isChargedOrNot = 'Battery is Charged to full';
boolShowAlert = true;
}else if(light){
if('$batteryState' == 'BatteryState.charging' && setBatteryPercentage <= intBatteryLevel){
isChargedOrNot = 'Battery is now over $setBatteryPercentage, please remove the charger!';
if(boolShowAlert){
_showAlert(context);
boolShowAlert = false;
}
}
}else{
isChargedOrNot = 'Battery is now charging and is at $intBatteryLevel%';
boolShowAlert = true;
}
});
}
@override
void dispose() {
timer.cancel();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Column(
children: [
Text(isChargedOrNot,
style: const TextStyle(
fontSize: 16.0,
fontWeight: FontWeight.bold,
letterSpacing: 2.0,
color: Colors.grey,
fontFamily: 'SomeTypeMono',
),
textAlign: TextAlign.center,
)
],
);
}
}
型
main.dart
import 'package:flutter/material.dart';
import 'package:my_battery_lives/widgets/Battery_Percentage_Text.dart';
import 'package:my_battery_lives/widgets/battery_is_charged_as_per_your_requirement.dart';
import 'package:my_battery_lives/widgets/turn_on_switch.dart';
void main() {
runApp(MaterialApp(
theme: ThemeData(
useMaterial3: true,
),
home: const Home(),
));
}
class Home extends StatefulWidget {
const Home({super.key});
@override
State<Home> createState() => _HomeState();
}
class _HomeState extends State<Home> {
String labelText = 'enable overcharge notification?';
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Battery Warning',
style: TextStyle(
fontFamily: 'SomeTypemono',
),
),
centerTitle: true,
),
body: Center(
child: Column(
children: [
const SizedBox(height: 180),
Text(labelText,
style: const TextStyle(
fontSize: 16.0,
fontWeight: FontWeight.bold,
letterSpacing: 2.0,
color: Colors.grey,
fontFamily: 'SometypeMono',
),
),
const SizedBox(
height: 20.0,
),
const TurnOnSwitchAndSlider(),
const BatteryPercentageText(),
const BatteryIsChargedAsPerYourRequirement(),
],
)
),
);
}
}
型
我试图从turn_on_switch.dart
到battery_is_charged_as_per_your_requirement.dart
获取光线
我知道我在做一些根本上错误的事情。我也是新来的。
1条答案
按热度按时间ybzsozfc1#
我阅读文档[在这里][https://pub.dev/packages/provider],看起来你没有为你的应用提供一个提供程序。你应该在你的主.dart
字符串