0

I'm returning user data and a token if the login data is correct. i want to pass the user data from the login page to other screens and use it within the app.

Below is the data i want to send to other screens...

data: {key: 4fd863897407cdf68e0ee073fcf08f7224d8aa25, user: {id: 2, first_name: Lina, last_name: Jorevaa, username: lina, email: lina@gmail.com, is_super_doc: false, is_doc: true}}

my mobile app ui is built with flutter while my rest api is built with django rest framework and rest-auth

*my login.dart*

Future<List> loginData() async {
    final response = await http
        .post('http://192.xxx.xxx.xxxx:42366/api/v1/rest-auth/login/', headers: {
      'Accept': 'application/json'
    }, body: {
      'email': '${emailController.text.trim().toLowerCase()}',
      'password': '${passwordController.text}'
    });

    setState(() {
      status = response.body.contains('error');
      data = json.decode(response.body);
      if (status) {
        setState(() {
          _showDialog();
        });
        print('data: ${data['error']}');
      } else {
        print('data: $data');
        Navigator.of(context).pushAndRemoveUntil(
          MaterialPageRoute(
            builder: (BuildContext context) => HomeScreen(
                  list: data,
                  index: 1,
                ),
          ),
          (Route<dynamic> route) => false,
        );
      }
    });
    return data;
  }
*my home.dart*
class HomeScreen extends StatelessWidget {
  final List list;
  final int index;

  HomeScreen({this.list, this.index});

This is the error i am getting

Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'FutureOr<List<dynamic>>'

Expecting clarification

Chanaka Weerasinghe
  • 5,404
  • 2
  • 26
  • 39
eddgachi
  • 11
  • 2

1 Answers1

0

This error makes sense because the function you've defined is of type Future. So, you should be returning a variable of same type i.e. Future<List>. Over here, 'data' is Map<String, dynamic>.

If you change the function type to Future<Map<String, dynamic>>, you'll be successfully returning the data from function loginData().

To pass data from one screen to other, you will have to define the class property in the destination class. And use it while calling the screen. Example here and here.

Sukhi
  • 13,261
  • 7
  • 36
  • 53