title: Null-Safety for List? Non-nullable Json Map from GET http request
body: <p>I am learning how to perform http requests in Dart / Flutter.</p>
  <p>I tried to perform a GET request from a simple api at <a href="https://jsonplaceholder.typicode.com/posts" rel="nofollow noreferrer">https://jsonplaceholder.typicode.com/posts</a></p>
  <p>I got the raw json, but need to parse &amp; map it out.</p>
  <p>My problem is the documentation I was following seems outdated, and the syntax I used with LIST no longer follows null-safety.</p>
  <ol>
  <li>Is there a better way to perform http requests in Dart / Flutter?</li>
  <li>Is there a better way to initialize my</li>
  </ol>
  <pre class="lang-dart prettyprint-override"><code>List&lt;DataModel&gt; model;
  </code></pre>
  <p>that allows for null-safety compliance without factoring a lot of code?</p>
  <p><strong>main.dart</strong></p>
  <pre class="lang-dart prettyprint-override"><code>
  import 'dart:convert';
  import 'package:flutter/material.dart';
  import 'package:http/http.dart' as http;
  
  import 'home_screen.dart';
  
  void main() =&gt; runApp(MyApp());
  
  class MyApp extends StatelessWidget {
    const MyApp({Key? key}) : super(key: key);
  
    @override
    Widget build(BuildContext context) {
      return const MaterialApp(
        home: HomeScreen(),
      );
    }
  }
  </code></pre>
  <p><strong>home_screen.dart</strong></p>
  <pre class="lang-dart prettyprint-override"><code>import 'package:flutter/material.dart';
  import 'package:http/http.dart' as http;
  import 'data_model.dart';
  import 'dart:convert' as convert;
  
  class HomeScreen extends StatefulWidget {
    const HomeScreen({Key? key}) : super(key: key);
  
    @override
    _HomeScreenState createState() =&gt; _HomeScreenState();
  }
  
  class _HomeScreenState extends State&lt;HomeScreen&gt; {
    List&lt;DataModel&gt; model;
  
    @override
    void initState() {
      getData();
      super.initState();
    }
  
    @override
    Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
              title: Text('Http Get Request'),
          ),
          body: ListView.builder(itemBuilder: (BuildContext context, int index){
            return const Card(
              child: ExpansionTile(
                  title: Text(model[index].title),
                  children: [Text(model[index].body)],
              ),
            );
          },
            itemCount: model.length,
          ),
        );
    }
  
    Future&lt;void&gt; getData() async {
      Uri url = Uri.https('jsonplaceholder.typicode.com', '/posts');
      http.Response response = await http.get(url);
      print(response.body);
      List&lt;dynamic&gt; body = convert.jsonDecode(response.body);
      model = body.map((dynamic item) =&gt; DataModel.fromJson(item)).toList();
      setState(() {
      });
    }
  }
  </code></pre>
  <p><strong>data_model.dart</strong></p>
  <pre class="lang-dart prettyprint-override"><code>class DataModel {
    late int userId;
    late int id;
    late String title;
    late String body;
  
    DataModel({required this.userId, required this.id, required this.title, required this.body});
  
    DataModel.fromJson(Map&lt;String, dynamic&gt; json) {
      userId = json['userId'];
      id = json['id'];
      title = json['title'];
      body = json['body'];
    }
  
    Map&lt;String, dynamic&gt; toJson() {
      final Map&lt;String, dynamic&gt; data = new Map&lt;String, dynamic&gt;();
      data['userId'] = this.userId;
      data['id'] = this.id;
      data['title'] = this.title;
      data['body'] = this.body;
      return data;
    }
  }
  </code></pre>