3

I'm making a CheckUpdate part which is check latest version on the web(json) and trying to compare. It parse JSON and converts into String. And then it try to compare with appBuildNumber. But it gives me error Failed Assertion: Boolean expression must not be null. Any ideas on the issue? P.S. Version checking server is https://raw.githubusercontent.com/TanzenT/LiteCalculator/master/version.json Code

class CalculatorPageHolder extends StatefulWidget {
  CalculatorPageHolder({Key key, this.title}) : super(key: key);
  final String title;

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

class _CalculatorPageHolderState extends State<CalculatorPageHolder> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      drawer: Drawer(child: mainDrawer()),
      body: Center(
          child: FutureBuilder<List<Version>>(
        future: fetchVersion(),
        builder: (context, snapshot) {
          if (snapshot.hasError) print(snapshot.error);

          return snapshot.hasData
              ? CalculatorHolder(version: snapshot.data)
              : Center(child: CircularProgressIndicator());
        },
      )),
    );
  }
}

class CalculatorHolder extends StatelessWidget {
  final List<Version> version;

  CalculatorHolder({Key key, this.version}) : super(key: key);

  Widget checkIsLatest() {
    print(version[0].version);
    if (getPackageInfo(version[0].version)) {
      return Column(
        mainAxisAlignment: MainAxisAlignment.end,
        children: <Widget>[Text('latest')],
      );
    } else {
      return Column(
        mainAxisAlignment: MainAxisAlignment.end,
        children: <Widget>[Text('not latest')],
      );
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(child: checkIsLatest());
  }
}

getPackageInfo(String _currnetVersion) {
  PackageInfo.fromPlatform().then((PackageInfo packageInfo) {
    String _buildNumber = packageInfo.buildNumber;
    print('Client Version : $_buildNumber');
    print('Server Version : $_currnetVersion');
    if (_buildNumber == _currnetVersion)
      return true;
    else
      return false;
  });
}
0

2 Answers 2

2

getPackageInfo isn't actually returning anything, so by default null is returned which isn't a valid boolean value.

You'll need to do something like this:

Future<bool> getPackageInfo(String _currentVersion) async {
  final packageInfo = await PackageInfo.fromPlatform();
  String _buildNumber = packageInfo.buildNumber;
  print('Client Version : $_buildNumber');
  print('Server Version : $_currentVersion');
  return _buildNumber == _currentVersion;
}

Note, this method returns a Future now and will need to be awaited before you can use the resulting boolean. You'll probably want to hoist that out of your widget and do it once since it probably shouldn't change during runtime as it's getting package information.

Sign up to request clarification or add additional context in comments.

Comments

2

You aren't actually returning a value from getPackageInfo. (The return statements are in the function you are passing to then.) So getPackageInfo returns null and your if statement where you test it fails.

Make getPackageInfo async, remove the then and use await PackageInfo.fromPlatform() - it can now return a Future<bool>. Of course, now you have to await the result, which you cannot do in build.

Consider moving the code to get the current package up into the stateful widget (call it from initState) and simply pass a bool depending on whether current or not down into CalculatorHolder instead of the version string.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.