Android 颤振,将日期时间值传递给方法

Android 颤振,将日期时间值传递给方法,android,flutter,datepicker,Android,Flutter,Datepicker,我目前有一个页面,我可以选择一个日期范围,之后我有一个搜索栏,然后是一个列表视图。 当我选择日期并单击提交时,会显示日期。 现在我需要将这些日期传递给一个方法,但它被接收为null。 似乎我忘记了一些东西来使用这个变量 class HistoriquePage extends StatefulWidget { final String title; final String namespace; const HistoriquePage({Key? key, required th

我目前有一个页面,我可以选择一个日期范围,之后我有一个搜索栏,然后是一个列表视图。 当我选择日期并单击提交时,会显示日期。 现在我需要将这些日期传递给一个方法,但它被接收为null。 似乎我忘记了一些东西来使用这个变量

class HistoriquePage extends StatefulWidget {
  final String title;
  final String namespace;

  const HistoriquePage({Key? key, required this.title, required this.namespace})
      : super(key: key);

  @override
  _HistoriquePageState createState() => _HistoriquePageState();
}

class _HistoriquePageState extends State<HistoriquePage> {
  GlobalKey<FormState> myFormKey = new GlobalKey();
  DateTimeRange? dateRange;
  Post? user;

  void _submitForm() {
    final FormState? form = myFormKey.currentState;
    form!.save();
  }

  @override
  Widget build(BuildContext context) => Scaffold(
        appBar: AppBar(
          title: Text(widget.title),
        ),
        body: SafeArea(
          child: Form(
            key: myFormKey,
            child: Column(
              children: [
                Container(
                  child: SafeArea(
                    child: DateRangeField(
                        enabled: true,
                        initialValue: DateTimeRange(
                            start: DateTime.parse("2020-01-01"),
                            end: DateTime.now()),
                        firstDate: new DateTime(2020),
                        decoration: InputDecoration(
                          labelText: 'Interval de temps pour la recherche',
                          prefixIcon: Icon(Icons.date_range),
                          border: OutlineInputBorder(),
                        ),
                        onSaved: (value) {
                          setState(() {
                            dateRange = value!;
                          });
                        }),
                  ),
                ),
                Container(
                  padding: EdgeInsets.all(16),
                  child: TextField(
                      decoration: InputDecoration(
                        prefixIcon: Icon(Icons.search),
                        border: OutlineInputBorder(),
                        labelText: 'Select the namespace...',
                      ),
                      onChanged: (searchedValue) {
                        setState(() {
                          HistoriqueService.filterList(searchedValue,
                              dateRange: dateRange);
                        });
                      }),
                ),
                ElevatedButton(
                  child: Text('Submit'),
                  onPressed: _submitForm,
                ),
                if (dateRange != null)
                  Text("Saved value is: ${dateRange.toString()}"),
                Expanded(child: DeploymentList())
              ],
            ),
          ),
        ),
      );
}

class HistoriqueService {
  static Future<List<User>> filterList(String value,
      {DateTimeRange? dateRange}) async {
    print("value" + value);
    if (dateRange != null) {
      print("dateRange: " + dateRange.toString());
    }
    List<User> postsList = await HttpService.fetchHistorique();
    return postsList
        .where((user) => user.name.toLowerCase().contains(value.toLowerCase()))
        .toList();
  }
}
class HistoryQuePage扩展StatefulWidget{
最后的字符串标题;
最后一个字符串名称空间;
const historyQuePage({Key?Key,需要this.title,需要this.namespace})
:super(key:key);
@凌驾
_HistoryQuePageState createState()=>\u HistoryQuePageState();
}
类_historyQuePageState扩展了状态{
GlobalKey myFormKey=新的GlobalKey();
DateTimeRange?dateRange;
邮政用户;
void _submitForm(){
最终FormState?form=myFormKey.currentState;
表单!.save();
}
@凌驾
小部件构建(构建上下文)=>Scaffold(
appBar:appBar(
标题:文本(widget.title),
),
正文:安全区(
孩子:表格(
密钥:myFormKey,
子:列(
儿童:[
容器(
儿童:安全区(
孩子:DateRangeField(
启用:对,
initialValue:DateTimeRange(
开始:DateTime.parse(“2020-01-01”),
结束:DateTime.now()),
firstDate:新日期时间(2020年),
装饰:输入装饰(
labelText:“重新开始的时间间隔”,
前缀:图标(图标。日期范围),
边框:OutlineInputBorder(),
),
已保存:(值){
设置状态(){
日期范围=值!;
});
}),
),
),
容器(
填充:边缘设置。全部(16),
孩子:TextField(
装饰:输入装饰(
前缀:图标(Icons.search),
边框:OutlineInputBorder(),
labelText:“选择名称空间…”,
),
一旦更改:(searchedValue){
设置状态(){
HistoryQueService.filterList(searchedValue,
日期范围:日期范围);
});
}),
),
升降按钮(
子项:文本('Submit'),
按下按钮:_submitForm,
),
如果(日期范围!=null)
Text(“保存的值为:${dateRange.toString()}”),
已展开(子项:DeploymentList())
],
),
),
),
);
}
类历史服务{
静态未来过滤器列表(字符串值,
{DateTimeRange?dateRange})异步{
打印(“值”+值);
如果(日期范围!=null){
打印(“dateRange:+dateRange.toString());
}
List postsList=wait HttpService.fetchhistoryque();
回邮单
.where((user)=>user.name.toLowerCase()包含(value.toLowerCase())
.toList();
}
}

您在课堂上使用了表单,但在这种情况下表单根本没有用处。 我建议您删除表单,然后将onSaved替换为onChanged,如下所示:

onChanged: (value) {
setState(() {
 dateRange = value!;
});
创建发送搜索值的方法:

void _sendSearch(String value) {
    _debounce.run(() {
      setState(() {
        searchedValue = value;
      });
    });
  }
现在可以替换命名空间的onChanged方法:

onChanged: _sendSearch
最后,将extanded方法替换为以下方法(现在将DateRange和searchedValue提供给部署列表:

Expanded(child: DeploymentList(searchedValue, dateRange: dateRange))

因此,每次更改日期范围或搜索值时,都将重建组件。

如果我理解正确,您可以执行
HistoryService。您的函数(日期范围)
在这种情况下,
HistoryService
final HistoryQueService HistoryService=HistoryService()生成
@SilkeNL这已经是我这里的了;HistoryQueService.filterList(searchedValue,dateRange:dateRange);你确定它设置了正确的状态吗?你能在更改后的
中打印dateRange:(searchedValue){setState((){HistoryQueService.filterList(searchedValue,dateRange:dateRange);}),
dataRange的状态在上面:
onSaved:(value){setState((){dateRange=value!;})
正如我所说,我可以在单击SubmitI understand时看到值,但代码中有些地方不正确。若要找出位置,请尝试在多个位置打印dateRange。日期在哪里,空在哪里?