Home > database >  type 'Null' is not a subtype of type 'UserMatch'
type 'Null' is not a subtype of type 'UserMatch'

Time:06-05

I am trying to navigate chat screen via UserMatch screen. I got the error on chat screen is "type 'Null' is not a subtype of type 'UserMatch'". I am a beginner in flutter. I really don't know how to tackle with it. This is the code of my UserMatch screen. I suppose the error that is coming should be on this screen.

import 'package:buis_talk/models/user_match_model.dart';
import 'package:buis_talk/screens/chat/chat_screen.dart';
import 'package:flutter/material.dart';

import '../../widgets/widgets.dart';

class MatchesScreen extends StatelessWidget {
  static const String routeName = '/matches';

  get userMatch => null;

  static Route route(){
    return MaterialPageRoute(
      settings: RouteSettings(name: routeName),
      builder: (context) => MatchesScreen(),
    );
  }

  @override
  Widget build(BuildContext context) {
    final inactiveMatches = UserMatch.matches
      .where((match) => match.userId == 1 && match.chat!.isEmpty)
      .toList();
    final activeMatches = UserMatch.matches
      .where((match) => match.userId == 1 && match.chat!.isNotEmpty)
      .toList();

    return Scaffold(
      appBar: const CustomAppBar(title: 'Matches'),
      body: SingleChildScrollView(
        child: Padding(
          padding: const EdgeInsets.all(20.0),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text('Your Likes', style: Theme.of(context).textTheme.headline5),
              SizedBox(
                height: 100,
                child: ListView.builder(
                  scrollDirection: Axis.horizontal,
                  shrinkWrap: true,
                  itemCount: inactiveMatches.length,
                  itemBuilder: (context, index) {
                    return Column(
                      children: [
                        UserImageSmall(
                          height: 64,
                          width: 64,
                          url: inactiveMatches[index].matchedUser.imageUrls
                        ),
                        Container( width: 16,),
                        Text(
                          inactiveMatches[index].matchedUser.name,
                          style: Theme.of(context).textTheme.bodyText1,
                        ),
                      ],
                    );
                  }
                ),
              ),
              const SizedBox(height: 10),
              const Divider(
                thickness: 3,
                indent: 3,
                endIndent: 3,
                color: Color.fromARGB(255, 190, 19, 19),
                height: 20,
              ),
              const SizedBox(height: 10),
              Text('Your Chats',
              style: Theme.of(context).textTheme.headline5),
              ListView.builder(
                shrinkWrap: true,
                // itemCount: activeMatches.length,
                itemCount: activeMatches.length,
                itemBuilder: (context, index) {
                  return InkWell (
                    onTap: () {
                      Navigator.push(
                        context, MaterialPageRoute(
                          // arguments: activeMatches[index],
                          builder: (context) => ChatScreen(userMatch: userMatch,),
                          
                        ),
                      );
                      // Navigator.pushNamed(context,'/chat',
                      //   arguments: activeMatches[index]);
                    },
                    child: Row(children: [
                      UserImageSmall(
                        height: 65,
                        width: 65,
                        url: activeMatches[index].matchedUser.imageUrls,
                        ),
                        Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Text(
                              activeMatches[index].matchedUser.name,
                              style: Theme.of(context).textTheme.headline6,
                            ),
                            const SizedBox(height: 7),
                            Text(
                              activeMatches[index].chat![0].messages[0].message,
                              style: Theme.of(context).textTheme.bodyText2,
                            ),
                            const SizedBox(height: 3),
                            Text(
                              activeMatches[index].chat![0].messages[0].timeString,
                              style: Theme.of(context).textTheme.bodyText2,
                            ),
                          ],
                        ),
                      ],
                    ),
                  );
                }
              )
            ],
          ),
        ),
      ),
    );
  }
} 

Chat screen is looking like this.

CodePudding user response:

You've written:

  get userMatch => null;

and then used it like this:

Navigator.push(
    context, MaterialPageRoute(
        builder: (context) => ChatScreen(userMatch: userMatch,),
        // I'm guessing this is what you want:
        // builder: (context) => ChatScreen(userMatch: activeMatches[index]),
    ),
);

ChatScreen expects a userMatch and you passed null into it. You should change the getter to return what you want instead of null.

  • Related