Flutter:NoSuchMethodError:方法'fetchByID'在null上被调用。接收方:null尝试调用:fetchByID(2)

问题描述 投票:0回答:1

我是Flutter的新手,这是我的第一个项目。我正在尝试使用参数导航到新站点。该参数被接受,但是我的类notizList中的函数返回错误消息。

我知道我的问题就在这行:Notiz notiz = key.currentState.fetchByID(widget.notizID);

错误消息是:

The method 'fetchByID' was called on null.
Receiver: null
Tried calling: fetchByID(2)

这些是相关文件。

app.dart

import 'style.dart';
import 'package:my_new_test_app/style.dart';
import 'Screens/home_screen.dart';
import 'Screens/notiz_detail.dart';

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      onGenerateRoute: _routes(),
      title: 'Notice',
      theme: _theme(),
    );
  }
}

ThemeData _theme() {
  return ThemeData(
    primaryColor: Colors.white,
  appBarTheme: AppBarTheme(
    textTheme : TextTheme(headline6: AppBarTextStyle)),
  textTheme: TextTheme(
  bodyText2: Body1Style,
  headline6: Title1Sytle),
);
}

RouteFactory _routes(){
  return(settings){
    final Map<String, dynamic> args = settings.arguments;
    Widget screen;
    switch (settings.name){
      case '/':
        screen = Test();
        return MaterialPageRoute(builder: (BuildContext context) => screen);
        break;
      case '/detailNotiz':
        screen = DetailNotiz(notizID: args['id']);
        return MaterialPageRoute(builder: (BuildContext context) => screen);
        break;
      default:
        return null;
    }
  };
}

notiz_detail.dart(问题出在哪里)

import 'package:my_new_test_app/models/notiz.dart';
import '../Widgets/detail_notiz_widget.dart';
import '../models/notizList.dart';


class DetailNotiz extends StatefulWidget {
const DetailNotiz({Key key, @required this.notizID}) : super(key: key);
final int notizID;

  @override
  _DetailNotizState createState() => _DetailNotizState();
}


class _DetailNotizState extends State<DetailNotiz> {

  final GlobalKey<NotizListeState> key = GlobalKey<NotizListeState>();
  @override
  Widget build(BuildContext context) {
    Notiz notiz = key.currentState.fetchByID(widget.notizID);
    return Scaffold(
      appBar: 
        AppBar(
          centerTitle: true,
          title: Text(notiz.user+' am '+notiz.date)
          ),
        body:Container(
      child: FullNotiz(notiz.title, notiz.body),
    ),
    );
  }
}

notizList.dart

import 'package:flutter/material.dart';
import 'notiz.dart';
import '../Widgets/little_notiz_widget.dart';


class NotizListe extends StatefulWidget {

  NotizListe({Key key}) : super(key: key);
  @override
  NotizListeState createState() => NotizListeState();
}


class NotizListeState extends State<NotizListe> {

static List<Notiz> notizList =[
      Notiz(
      1,
      'USer', 
      "Title",
      "BOdy",
      "01.05.2020"
      ),
      Notiz(
       2,
       'USer', 
      "Title",
      "BOdy",
       "03.05.2020"
      ),
      Notiz(
       3,
       'USer', 
      "Title",
      "BOdy",
       "03.05.2020"
      ),
      Notiz(
       4,
       'USer', 
      "Title",
      "BOdy",
       "04.05.2020"
      ),
      Notiz(
       5,
       'USer', 
      "Title",
      "BOdy",
       "04.05.2020"
      ),

    ];


  void addToList(String title, String body){
    int index = notizList.length +1;
    Notiz element = Notiz(index, 'Marc', title, body, '05.05.2020');
    notizList.add(element);
  }

  Notiz fetchByID(int notizID){
    print('Die Länge der notiz Liste: '+ notizList.length.toString());
    print(notizID);
    for (var i = 0; i<notizList.length;i++){
      if(notizList[i].id == notizID){
        return notizList[i];
      }
    }
    return notizList[1];
  }

  @override
  Widget build(BuildContext context) {
    return notizenListView(context, notizList);
  }

  Widget notizenListView(BuildContext context, List<Notiz> notizList){
    return ListView.builder(
      itemCount: notizList.length,
      itemBuilder: (context, index) => 
          _itemBuilder(context, notizList[index]),
      );
  }
}



_onNotizTap(BuildContext context, int notizenId){
  Navigator.pushNamed(context,  '/detailNotiz', arguments: {"id": notizenId});
}

  Widget _itemBuilder(BuildContext context, Notiz notizen){

    return GestureDetector(
      onTap: ()=>_onNotizTap(context, notizen.id),
      key: Key('notizen_list_item_${notizen.id}'),
      child: Container(
        child: LittleNotiz(notizen.title, notizen.body),
      )
    );
  }


感谢您的帮助。

flutter dart error-handling nosuchmethoderror nosuchmethod
1个回答
0
投票

您没有在_DetailNotizState中调用NotizListeState,这就是为什么会出错的原因。

© www.soinside.com 2019 - 2024. All rights reserved.