0

I'am new in Flutter, and this is my first project. I'am trying to navigate to the new site with parameters. The parameter is accepted but the function in my class notizList returns an error message.

I know that my Problem is at the line : Notiz notiz = key.currentState.fetchByID(widget.notizID);

The error message is :

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

These are the relevant files.

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 (wheres the problem)

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),
      )
    );
  }


Thanks for your Help.

Marc
  • 13
  • 1

1 Answers1

1

You are not calling NotizListeState in _DetailNotizState, that's why you are getting error.

As you are not using NotizListeState, so you can't pass key to NotizListe because of that you are getting this error.

Moreover, until build method doesn’t call you can't using key because it doesn’t assign key to that widget.

full code Demo:

class DeleteWidget extends StatefulWidget {
  @override
  _DeleteWidgetState createState() => _DeleteWidgetState();
}

class _DeleteWidgetState extends State<DeleteWidget> {
  hi() {
    key.currentState.callme();
  }

  final GlobalKey<Home1State> key = GlobalKey<Home1State>();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: Container(
        child: Column(
          children: [
            RaisedButton(
              onPressed: hi,
              child: Text("hello"),
            ),
            Home1(
              key: key,
            ),
          ],
        ),
      ),
    );
  }
}

class Home1 extends StatefulWidget {
  Home1({Key key}) : super(key: key);
  @override
  Home1State createState() => Home1State();
}

class Home1State extends State<Home1> {
  callme() {
    print("object");
  }

  @override
  Widget build(BuildContext context) {
    return Container();
  }
}
Viren V Varasadiya
  • 17,353
  • 7
  • 30
  • 46