gpt4 book ai didi

dart - 错误 : The argument type '() → Null' can't be assigned to the parameter type '(Null) → FutureOr'

转载 作者:IT王子 更新时间:2023-10-29 06:54:23 28 4
gpt4 key购买 nike

    import 'package:flutter/material.dart';
import 'package:camera/camera.dart';


class Register extends StatefulWidget{
List<CameraDescription> cameras;
@override
_Register createState() {
return _Register();
}

Register(this.cameras);
}

class _Register extends State<Register>{

CameraController controller;

@override
Widget build(BuildContext context){
return MaterialApp(
title: 'Registration Certificate',
home: Scaffold(
appBar: AppBar(
title: new Text('Register'),
),

body: Container()
),
);
}


@override
void initState(){
super.initState();
controller = new CameraController(widget.cameras[0], ResolutionPreset.medium);
//**SYNTAX ERROR from the below code**
controller.initialize().then(() {});
}
}

错误详情

error: The argument type '() → Null' can't be assigned to the parameter type '(Null) → FutureOr'. (argument_type_not_assignable at [fluttercam] lib\packs\reg.certificate.dart:38)

当我使用“_”下划线时,代码运行正常,错误已解决!

controller.initialize().then((_) {});

谁能解释一下幕后发生的事情?

最佳答案

当需要一个带参数的函数时,

(Null) → FutureOr<dynamic>

你不能传递一个不接受任何参数的函数

() → Null

_是一个有效的参数名称,按照惯例用于指示未使用该参数并使传递的函数与定义的参数类型兼容,因为它现在接受一个参数。

controller.initialize()返回 Futurethen(...)方法需要一个在异步任务完成时调用的函数,并且已完成的异步任务的结果作为参数传递给该回调函数。

https://api.dartlang.org/stable/2.1.0/dart-async/Future/then.html显示 then 的参数定义:

FutureOr<R> onValue(T value)

这是一个接受泛型参数的函数 T并返回值 FutureOr<R>

关于dart - 错误 : The argument type '() → Null' can't be assigned to the parameter type '(Null) → FutureOr<dynamic>' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53475734/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com