'When specifying MainAxisAligment as key value in json i get null value

I have a use case where i need to specify the aligment of certain images with json object

This is the json object where i specify the aligment:

# json_object.json  

{
  "aligment" : "center"
}

I also have a method in another file where i grab this json object:

# get_json_object.dart

class GetJsonObject{
  Future<Map<String, dynamic>> getJson() async {
    String jsonData =
        await rootBundle.loadString('assets/json_object.json');
    Map<String, dynamic> data = jsonDecode(jsonData);
    return data;
  }


}

I also have a another method where i do if else statement to specify which key ther is in key aligment from the json object. I first grab the mapped json from GetJsonObject().getJson() and then return the MainAxisAlignment based on the key value from key alignment.:

# get_alignment.dart

class GetAlignment {
   Future getAxisAlignment(String key) async {
    var mappedJson = await GetJsonObject().getJson();

    if (mappedJson[key] == mappedJson["start"]) {
      return MainAxisAlignment.start;
    } else if (mappedJson[key] == mappedJson["center"]) {
      return MainAxisAlignment.center;
    } else if (mappedJson[key] == mappedJson["end"]) {
      return MainAxisAlignment.end;
    }
  }
}

For the view i have stateful widget where i grab the return type MainAxisAlignment from GetAlignment().getAxisAlignment() from the file get_alignment.dart and use setState to set the return type from GetAlignment().getAxisAlignment() to the field axisAlignment. I then use field axisAlignment for the position of the FlutterLogo() with Row():


# test_view.dart


class TestView extends StatefulWidget {
  const TestView({Key? key}) : super(key: key);

  @override
  State<TestView> createState() => _TestViewState();
}

class _TestViewState extends State<TestView> {
  late MainAxisAlignment axisAlignment = MainAxisAlignment.start;


  setAlignment() async {
    final alignment =
    await GetAligmnent().getAlignment("alignment");

    setState(() {
      axisAlignment = alignment;
      
    });
  }

  @override
  initState() {
    setAlignment();
    super.initState();
  }


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: Center(
        child: Row(
          mainAxisAlignment: axisAlignment,
          children: [
            FlutterLogo()
          ],
        ),
      )
    );
  }
}

But for some reason i get error in setState(): E/flutter ( 9246): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: type 'Null' is not a subtype of type 'MainAxisAlignment'

So the return type is Null value, but i don't understand why.



Solution 1:[1]

I found out why i got Null as return type from getAxisAlignment. The reason for this was that i used wrong format inside the if else. I had to change mappedJson["start"] to just plain String like this:

  Future getAlignment(String key) async {
    var settings = await clientSettings;

    if (settings[key] == "start") {
      return MainAxisAlignment.start;
    } else if (settings[key] == "center") {
      return MainAxisAlignment.center;
    } else if (settings[key] == "end") {
      return MainAxisAlignment.end;
    } else {
      return MainAxisAlignment.center;
    }
  }

And i added default return type to get rid of the error when the return type was Null

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 FDjawid