Home > Blockchain >  Flutter 2.8 : Passing callback function to child from parent
Flutter 2.8 : Passing callback function to child from parent

Time:02-17

I had a login widget on older version of Flutter , where I passed the callback function to child from parent and set the login status. Now I updated to Flutter 2.8 , this doesn't allow me to pass the function and I am getting an error :

Error: The parameter 'isSign' can't have a value of 'null' because of its type 'void Function()', but the implicit default value is 'null'.
Try adding either an explicit non-'null' default value or the 'required' modifier.

Here is my main component:

 void _signInStatus() {
    getToken().then((value) {
      if (token == null) {
        setState(() {
          _isLoggedIn = false;
        });
      } else {
        setState(() {
          _isLoggedIn = true;
        });
      }
    });
  }

@override

 Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        body: _isLoggedIn? RootPage(): SignInPage(isSign : _signInStatus),
      ),
    );
  }

Here is my SignInPage

class SignInPage extends StatefulWidget {
  SignInPage({this.isSign});
  final VoidCallback isSign;

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

I tried to check official documentation and could not find anything. Please help.

CodePudding user response:

Your isSign method is not nullable, so it should be marked as required and you should not be able to pass any null value to it.

class SignInPage extends StatefulWidget {
  SignInPage({required this.isSign});
  final VoidCallback isSign;

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

EDIT:

Since you do not return anything, your function return type is the only thing that matters here. You can see that it does not have anything to do with that being void by the example below. You can also run it on dartpad.dev

import 'package:flutter/material.dart';

const Color darkBlue = Color.fromARGB(255, 18, 32, 47);

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  void callMe() {
    print('this is called');
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData.dark().copyWith(
        scaffoldBackgroundColor: darkBlue,
      ),
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        body: Center(
          child: MyWidget(
            isSign: callMe,
          ),
        ),
      ),
    );
  }
}

class MyWidget extends StatelessWidget {
  const MyWidget({required this.isSign});
  final VoidCallback isSign;
  @override
  Widget build(BuildContext context) {
    isSign();
    return Text(
      'Hello, World!',
      style: Theme.of(context).textTheme.headline4,
    );
  }
}

CodePudding user response:

Remove void from the method or edit it like the following

bool _signInStatus() {
getToken().then((value) {
  if (token == null) {
    setState(() {
      _isLoggedIn = false;
    });
  } else {
    setState(() {
      _isLoggedIn = true;
    });
  }
});
  return true;
 }
  • Related