Develop/Flutter

[Flutter][오류]Error: The argument type 'Function?' can't be assigned to the parameter type 'void Function()?'

dawonny 2022. 7. 24. 20:24
728x90
반응형

버튼 위젯을 따로 파일로 만들어서 여러 파일들에서 쓰던 중에

Error: The argument type 'Function?' can't be assigned to the parameter type 'void Function()?'

오류가 났다.

onPressed 부분이 문제가 되는 것 같았다.

이렇게 bar_button 이라는 파일이 있고 

다른 파일에서 이렇게 응용하던 중이었다.


해결

문제를 해결하는데에 이 영상이 도움이 되었다.

https://www.youtube.com/watch?v=YanWDEuISRI&ab_channel=IntelliLogics 

 

아니면 오류에서 제안하는 대로 onPressed 변수 앞에

void Function()? 을 붙이면 오류가 해결된다.

코드는 아래와 같다.

import 'package:flutter/material.dart';
import 'package:provider/single_child_widget.dart';

class BarButton extends StatelessWidget {
  final Widget child;
  final void Function()? onPressed;
  const BarButton({
    Key? key,
    required this.child,
    required this.onPressed,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ElevatedButton(
        onPressed: onPressed,
        child: child);
  }
}
728x90
반응형