类型 'Null' 不是类型 'UserMatch' 的子类型

type 'Null' is not a subtype of type 'UserMatch'

我正在尝试通过 UserMatch 屏幕浏览聊天屏幕。我在聊天屏幕上得到的错误是“类型 'Null' 不是类型 'UserMatch' 的子类型”。我是颤振的初学者。我真的不知道该如何应对。 这是我的 UserMatch 屏幕的代码。我想即将出现的错误应该出现在这个屏幕上。

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.

您写了:

  get userMatch => null;

然后像这样使用它:

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

ChatScreen 需要一个 userMatch,而您将 null 传递给它。您应该将 getter 更改为您想要的 return 而不是 null。