我有这样一个问题,我想把Flutter和.Net核心Web API集成在一起。我是新来的Flutter所以,我只是按照教程,但错误在这里的对象是需要初始化第一这是我的片段,我不知道该把什么**(编辑:假,服务:空)**
floatingActionButton: FloatingActionButton(
onPressed: () => Navigator.of(context).pushNamed(
AddUpdateService.routeName,
arguments: ServiceArgument(edit: false, service: null),
),
child: const Icon(Icons.add),
),
service这里是我想在AddUpdateService中添加或更新的对象,下面是AddUpdateService的代码
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import '../bloc/bloc.dart';
import '../model/models.dart';
import 'ScreenRoute.dart';
import 'ServiceMainScreen.dart';
class AddUpdateService extends StatefulWidget {
static const routeName = 'courseAddUpdate';
final ServiceArgument args;
const AddUpdateService({super.key, required this.args});
@override
_AddUpdateServiceState createState() => _AddUpdateServiceState();
}
class _AddUpdateServiceState extends State<AddUpdateService> {
final _formKey = GlobalKey<FormState>();
final Map<String, dynamic> _service = {};
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.args.edit ? "Edit Course" : "Add New Course"),
),
body: Padding(
padding: const EdgeInsets.all(8.0),
child: Form(
key: _formKey,
child: Column(
children: [
TextFormField(
initialValue: widget.args.edit ? widget.args.service.ServiceName : '',
validator: (value) {
if (value!.isEmpty) {
return 'Please enter course code';
}
return null;
},
decoration: const InputDecoration(labelText: 'Service Name'),
onSaved: (value) {
setState(() {
_service["serviceName"] = value;
});
}),
TextFormField(
initialValue:
widget.args.edit ? widget.args.service.Description : '',
validator: (value) {
if (value!.isEmpty) {
return 'Please enter service Description';
}
return null;
},
decoration: const InputDecoration(labelText: 'Description'),
onSaved: (value) {
_service["description"] = value;
}),
TextFormField(
initialValue: widget.args.edit
? widget.args.service.Category
: '',
validator: (value) {
if (value!.isEmpty) {
return 'Please enter Service Category';
}
return null;
},
decoration: const InputDecoration(labelText: 'Category'),
onSaved: (value) {
setState(() {
_service["category"] = value;
});
}),
TextFormField(
initialValue: widget.args.edit
? widget.args.service.InitialPrice.toString()
: '',
validator: (value) {
if (value!.isEmpty) {
return 'Please enter Service Initial Pice';
}
return null;
},
decoration: const InputDecoration(labelText: 'InitialPrice'),
onSaved: (value) {
setState(() {
_service["initialPrice"] = int.parse(value!);
});
}),
TextFormField(
initialValue: widget.args.edit
? widget.args.service.IntermediatePrice.toString()
: '',
validator: (value) {
if (value!.isEmpty) {
return 'Please enter Service Intermediate Pice';
}
return null;
},
decoration: const InputDecoration(labelText: 'Intermediate'),
onSaved: (value) {
setState(() {
_service["intermediatePrice"] = int.parse(value!);
});
}),
TextFormField(
initialValue: widget.args.edit
? widget.args.service.AdvancedPrice.toString()
: '',
validator: (value) {
if (value!.isEmpty) {
return 'Please enter Service Advanced Pice';
}
return null;
},
decoration: const InputDecoration(labelText: 'AdvancedPrice'),
onSaved: (value) {
setState(() {
_service["advancedPrice"] = int.parse(value!);
});
}),
Padding(
padding: const EdgeInsets.symmetric(vertical: 16.0),
child: ElevatedButton.icon(
onPressed: () {
final form = _formKey.currentState;
if (form!.validate()) {
form.save();
final ServiceEvent event = widget.args.edit
? ServiceUpdate(
Service(
id: widget.args.service.id,
ServiceName: _service["serviceName"],
Description: _service["description"],
Category: _service["category"],
InitialPrice: _service["initialPrice"],
IntermediatePrice: _service["intermediatePrice"],
AdvancedPrice: _service["advancedPrice"], imageUrl: '',
),
)
: ServiceCreate(
Service(
ServiceName: _service["serviceName"],
Description: _service["description"],
Category: _service["category"],
InitialPrice: _service["initialPrice"],
IntermediatePrice: _service["intermediatePrice"],
AdvancedPrice: _service["advancedPrice"], id: 123, imageUrl: '',
),
);
BlocProvider.of<ServiceBloc>(context).add(event);
Navigator.of(context).pushNamedAndRemoveUntil(
ServiceMainScreen.routeName, (route) => false);
}
},
label: const Text('SAVE'),
icon: const Icon(Icons.save),
),
),
],
),
),
),
);
}
}
如何在将输入放入下一页(AddUpdateService)之前将初始对象设置为null,下面是参数类
class ServiceArgument {
final Service service;
final bool edit;
ServiceArgument({required this.edit, required this.service});
}
如何解决这个问题
1条答案
按热度按时间ymdaylpp1#
Flutter是null-safe的,这意味着默认情况下变量不能为null,除非你将其指定为nullable。您可以通过在类型后面添加
?
来完成此操作。所以改变至
如果您希望允许将
null
放入service