title: Flutter return a message after successful Api call
body: <p>Hello i am new to flutter and i decided to make TodoList app to sharpen my skills and getting used to flutter and working with api </p>
  
  <p><strong>what i am trying to do</strong> is that i want to show a message written in our language after submiting data to the api </p>
  
  <p>the code </p>
  
  <pre><code>Future&lt;Todo&gt; sendData(String title, String body) async{
    final http.Response response = await http.post(
      'http://192.168.1.20/Todo_api/public/todos/store',
      headers: &lt;String , String&gt;{
        'Content-Type' : 'application/json; charset=UTF-8',
      },
      body: jsonEncode(&lt;String,String&gt;{
        'title' : title,
        'body' : body,
      }),
    );
    if(response.statusCode == 200){
      //200 Created
      //parse json
      return Todo.fromJson(jsonDecode(response.body)) ;
    }else{
      throw Exception('Failed to submit Contact the Devolopers please ');
    }
  }
  </code></pre>
  
  <p>as you can see i want to show a message for the user if the code is 200 (success) instead of parsing the json response to the user because he wouldn't get it and also i want to call Navigator.pop() so that the user might go back to the main screen 
  i tried return function,widget,string,...etc but it didn't work and kept throwing this error</p>
  
  <blockquote>
    <p>error: A value of type 'Null Function() can't be returned from
    function 'sendData' because it has a return type of 'Future'.
    (return_of_invalid_type at [todolist] lib\CreateTodo.dart:35)</p>
  </blockquote>
  
  <p>so what i want to do as mentioned before i want to go back to the main screen and show a friendly message for the user like in laravel </p>
  
  <pre><code>return redirect("/")-&gt;with('success' , 'Todo Stored Successfully');
  </code></pre>
  
  <p>and thats it 
  Thanks for taking a look at my post and Thanks in advance</p>
  
  <p><strong>UPDATE : Solved it by doing if statement on my variables</strong></p>
  
  <p>full code </p>
  
  <pre><code>import 'dart:convert';
  import 'dart:async';
  import 'package:flutter/material.dart';
  import 'package:http/http.dart' as http;
  import 'package:rflutter_alert/rflutter_alert.dart';
  import 'main.dart';
  class Todo {
    final title;
    final body;
    Todo ({this.title , this.body});
  
    factory Todo.fromJson(Map&lt;String , dynamic&gt; json){
      return Todo(
        title: json['title'],
        body: json['body'],
  
      );
    }
  }
  
  Future&lt;Todo&gt; sendData(String title, String body) async{
    final http.Response response = await http.post(
      'http://192.168.1.20/Todo_api/public/todos/store',
      headers: &lt;String , String&gt;{
        'Content-Type' : 'application/json; charset=UTF-8',
      },
      body: jsonEncode(&lt;String,String&gt;{
        'title' : title,
        'body' : body,
      }),
    );
    if(response.statusCode == 201){
      return Todo.fromJson(jsonDecode(response.body));
    }else{
      return null;
    }
  }
  
  
  
  class CreateTodo extends StatefulWidget {
    @override
    _CreateTodoState createState() =&gt; _CreateTodoState();
  }
  
  class _CreateTodoState extends State&lt;CreateTodo&gt; {
    Future&lt;Todo&gt; _futureTodo;
  
    String title = "";
  
    String body = "";
  
    @override
    Widget build(BuildContext context) {
      return Scaffold(
        appBar: AppBar(
          title: Text("Create Todo"),
        ),
        body: Center(
          child : ListView(
            children: &lt;Widget&gt;[
                 Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: TextField(
                    onChanged: (text){
                      title = text;
                    },
                    decoration: InputDecoration(hintText: 'Enter Title'),
                  ),
                ),
              Padding(
                padding: const EdgeInsets.all(8.0),
                child: TextField(
                  onChanged: (text){
                    body = text;
                  },
                  decoration: InputDecoration(hintText: 'Enter Body'),
                ),
              ),
              IconButton(
                icon: Icon(Icons.send),
                  //if contains data then send else block
                  onPressed: () {
                      if (title == '' || body == ''){
                        Alert(context: context, title: "Error", desc: "Please fill the form", type: AlertType.error).show();
                      }else{
                        _futureTodo = sendData(title,body);
                        Alert(context: context, title: "Success", desc: "Stored Successfully", type: AlertType.success,
                            buttons: [
                              DialogButton(
                                child: Text(
                                  "Home",
                                  style: TextStyle(color: Colors.white, fontSize: 20),
                                ),
                                onPressed: (){
                                  Navigator.push(
                                    context,
                                    MaterialPageRoute(builder: (context) =&gt; MyApp()),
                                  );
                                },
                                width: 120,
                              ),
                              DialogButton(
                                child: Text(
                                  "Back",
                                  style: TextStyle(color: Colors.white, fontSize: 20),
                                ),
                                onPressed: (){
                                  Navigator.push(
                                    context,
                                    MaterialPageRoute(builder: (context) =&gt; CreateTodo()),
                                  );
                                  },
                              ),
                            ]
                        ).show();
                      }
                  },
              ),
            ],
          ),
        ),
      );
    }
  }
  </code></pre>