如何解决Flutter 错误:方法 toDouble() 在 null 上被调用,尝试调用 toDouble
我遇到了 import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.*;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import javax.swing.*;
import javax.swing.event.changelistener;
public class SimpleLotMain {
public static void main(String[] args) {
SwingUtilities.invokelater(() -> {
LotGui gui = new LotGui(20);
JFrame frame = new JFrame("Lot");
frame.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
frame.add(gui);
frame.pack();
frame.setLocationRelativeto(null);
frame.setVisible(true);
});
}
}
@SuppressWarnings("serial")
class LotGui extends JPanel {
private Map<String,ParkingSlot> slotMap = new LinkedHashMap<>();
public LotGui(int slotCount) {
JPanel gridPanel = new JPanel(new GridLayout(0,1));
for (int i = 0; i < slotCount; i++) {
String slotName = String.format("V%03d",i);
ParkingSlot slot = new ParkingSlot(slotName);
slotMap.put(slotName,slot);
JButton button = new JButton(slotName + ": Empty");
button.setActionCommand(slotName);
button.addActionListener(e -> parkingSlotButtonAction(e));
gridPanel.add(button);
}
JScrollPane scrollPane = new JScrollPane(gridPanel);
scrollPane.getViewport().setPreferredSize(new Dimension(400,400));
JButton displaySlotsBtn = new JButton("display Parking Spots");
displaySlotsBtn.addActionListener(e -> displaySlots());
JPanel bottomPanel = new JPanel();
bottomPanel.add(displaySlotsBtn);
setLayout(new BorderLayout());
add(scrollPane);
add(bottomPanel,BorderLayout.PAGE_END);
}
private void parkingSlotButtonAction(ActionEvent e) {
String slotName = e.getActionCommand();
JButton source = (JButton) e.getSource();
ParkingSlot slot = slotMap.get(slotName);
// JOptionPane to query whether to add/remove car
String carName = JOptionPane.showInputDialog("Enter Name of Car to Add");
// if the user cancels the dialog or enters only white-space text: exit
if (carName == null || carName.trim().isEmpty()) {
return;
}
// else,use the name
Car car = new Car(carName);
slot.setCar(car);
String text = slotName + ": " + carName;
source.setText(text);
}
private void displaySlots() {
for (Entry<String,ParkingSlot> entry : slotMap.entrySet()) {
System.out.printf("%s%n",entry.getValue());
}
}
}
//class SLot {
// List<ParkingSlot> slots = new ArrayList<>();
//
// public SLot(int carCount) {
// for (int i = 0; i < carCount; i++) {
// String name = String.format("S%03d",i);
// slots.add(new ParkingSlot(name));
// }
// }
//
// @Override
// public String toString() {
// return "SLot [slots=" + slots + "]";
// }
//
// public void addchangelistener(changelistener l) {
//
// }
//}
class Car {
private String name;
public Car(String name) {
this.name = name;
}
public String getName() {
return name;
}
@Override
public String toString() {
return "Car: " + name;
}
}
class ParkingSlot {
private Car car;
private String name;
public ParkingSlot(String name) {
this.name = name;
}
public void setCar(Car car) {
this.car = car;
}
public Car getCar() {
return car;
}
public void clear() {
car = null;
}
public String getName() {
return name;
}
@Override
public String toString() {
String value = "Slot: " + name + "; ";
if (car == null) {
value += "empty";
} else {
value += car.toString();
}
return value;
}
}
错误。我已经多次检查我的代码,但我无法弄清楚发生了什么。
todouble was called on null
Slider Function 中的 import 'package:Flutter/material.dart';
import 'package:freezeria/Screens/services/database.dart';
import 'package:freezeria/common_code/common.dart';
import 'package:freezeria/common_code/loader.dart';
import 'package:freezeria/req_info/shakes.dart';
import 'package:freezeria/req_info/user.dart';
import 'package:provider/provider.dart';
class bottomSheetPanel extends StatefulWidget {
@override
_bottomSheetPanelState createState() => _bottomSheetPanelState();
}
class _bottomSheetPanelState extends State<bottomSheetPanel> {
final _formkey = GlobalKey<FormState>();
final List<String> blending = ['1','2','3','4','5'];
final List<String> cupsize = ['Select','Small','Medium','Large'];
final List<String> flavour = [
'Chocolate','Vanilla','StrawBerry','BlueBerry','RaspBerry','Kiwi','Select'
];
final List<String> cream = [
'Whipped','Caramel','Chocolate','Select'
];
final List<String> toppings = [
'Oreo','Cherry','Sweet Lime','Waffle','Blackberries','Select'
];
String _currentName;
String _currentCupSize;
int _currentBlend;
String _currentFlavour;
String _currentCream;
String _currentToppings;
@override
Widget build(BuildContext context) {
final receiveUser = Provider.of<User>(context);
return StreamBuilder<UserData>(
stream: Database(uid: receiveUser.uid).userDataStream,builder: (context,snapshot) {
if (snapshot.hasData) {
UserData recentData = snapshot.data;
return Form(
key: _formkey,child: SingleChildScrollView(
child: Column(
children: [
Text(
"Update Your Data",style: TextStyle(fontSize: 20.0),),SizedBox(
height: 20.0,TextFormField(
initialValue: recentData.Name,decoration:
decorationCode.copyWith(hintText: 'Enter your name'),validator: (val) =>
val.isEmpty ? 'Please enter a name' : null,onChanged: (val) => setState(() {
return _currentName = val;
}),DropdownButtonFormField(
value: _currentCupSize ?? recentData.CupSize,items: cupsize.map((size) {
return DropdownMenuItem(
value: size,child: Text('$size Size'),);
}).toList(),onChanged: (val) => setState(() {
return _currentCupSize = val;
}),Slider(
value: (_currentBlend ?? recentData.Blend).todouble(),activeColor:
Colors.grey[_currentBlend ?? recentData.Blend],inactiveColor:
Colors.grey[_currentBlend ?? recentData.Blend],min: 100.0,max: 700.0,divisions: 6,onChangeEnd: (val) => setState(() {
return _currentBlend = val.round();
}),DropdownButtonFormField(
value: _currentFlavour ?? recentData.Flavour,items: flavour.map((flav) {
return DropdownMenuItem(
value: flav,child: Text('$flav Flavour'),onChanged: (val) => setState(() {
return _currentFlavour = val;
}),DropdownButtonFormField(
value: _currentCream ?? recentData.Cream,items: cream.map((crem) {
return DropdownMenuItem(
value: crem,child: Text('$crem Cream'),onChanged: (val) => setState(() {
return _currentCream = val;
}),DropdownButtonFormField(
value: _currentToppings ?? recentData.Toppings,items: toppings.map((top) {
return DropdownMenuItem(
value: top,child: Text('$top Toppings'),onChanged: (val) => setState(() {
return _currentToppings = val;
}),ElevatedButton(
onpressed: () async {
print(_currentName);
print(_currentBlend);
print(_currentCupSize);
print(_currentFlavour);
print(_currentToppings);
},style: ButtonStyle(
backgroundColor: MaterialStateProperty.all<Color>(
Colors.purple[300]),foregroundColor:
MaterialStateProperty.all<Color>(Colors.white),child: Text(
'UPDATE',style: TextStyle(color: Colors.white),)
],);
} else {
Loading();
}
});
}
}
出错,todouble()
也没有更新底部工作表中的名称。
发现 recentData.name
和 _currentBlend
给出了空值,我该如何更正?
任何帮助将不胜感激:)
解决方法
如果有可能,你会使用常量值。
喜欢:
value: (_currentBlend ?? 0).toDouble(),
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。