1

所以我制作了这个颤振应用程序,它将单词翻译成baybayin(仍然有关于它的错误,请也帮助我)。该应用程序将有一个使用 Hive 的数据库。它工作得很好。当我重新加载我的应用程序的一部分时,我设法做到了。然后我注意到前几天,当我应该继续设计我的应用程序时,我收到了这个错误

════════ Exception caught by widgets library ═══════════════════════════════════════════════════════
The following NoSuchMethodError was thrown building TranslatePage(dirty, state: _TranslatePageState#347de):
The method 'get' was called on null.
Receiver: null
Tried calling: get(1)

The relevant error-causing widget was: 
  TranslatePage file:///C:/Users/me/AndroidStudioProjects/baybay_app/lib/Translate/Translate.dart:35:18
When the exception was thrown, this was the stack: 
#0      Object.noSuchMethod (dart:core-patch/object_patch.dart:51:5)
#1      _TranslatePageState.build (package:baybay_app/Translate/Translate.dart:189:35)
#2      StatefulElement.build (package:flutter/src/widgets/framework.dart:4744:28)
#3      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4627:15)
#4      StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4800:11)
...
════════════════════════════════════════════════════════════════════════════════════════════════════

我尝试了在谷歌上找到的方法,我尝试了很多方法,但我总是到这里。如果我重新加载它,它工作正常。但是当我退出翻译药水时(我转到我的 Baybayin 应用程序的主页),它再次显示。每次我重新加载它时它都可以正常工作。当我热启动它时,错误再次显示。我不能一直重新加载它,尤其是当我释放它时,

这是我的代码。如您所见,我尝试了多种方法来解决该错误。我的代码可能不干净。因为我尝试了多种方法。

class Translate extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
      future:
      DefaultAssetBundle.of(context).loadString(
          'assets/lang/tl-Fil.json', cache: false),
      builder: (context, snapshot) {
        List mydata = json.decode(snapshot.data.toString());
        if (mydata == null) {
          return Scaffold(
            body: Center(
              child: Text(
                "Loading",
              ),
            ),
          );
        } else {
          return TranslatePage(mydata: mydata);
        }
      },
    );
  }

}

class TranslatePage extends StatefulWidget {

  final List mydata;
  // final List<TranslatePageLetter> _TranslatePageLetter = TranslatePageletter;
  TranslatePage({Key key, @required this.mydata}) : super(key: key);
  @override
  _TranslatePageState createState() => _TranslatePageState(mydata);
}

class _TranslatePageState extends State<TranslatePage> {
  Box box;


// final letters = [
//   let(
//     englet: "ba",
//     bylet: "b"
//   ),
//   let(englet: "be", bylet: "be"),
//   let(englet: "bi", bylet: "bi"),
//   let(englet: "bo", bylet: "bo"),
//   let(englet: "bu", bylet: "bu"),
//   let(englet: "b", bylet: "b+")
// ];
// List<let> letter;

  // @override
  //  void initState() {
  //    super.initState();
  //
  //  }
  Future openBox() async {
    var dir = await getApplicationDocumentsDirectory();
    Hive.init(dir.path);
    box = await Hive.openBox('translate');
    return;
  }

  final List mydata;

  _TranslatePageState(this.mydata);

  final translate = TextEditingController();
  String tagalog;
  String translateoutput = "Baybay";
  var translatedphrase = '';
  int charLength = 0;
  String text;
  int even = 2;
  bool istyping = false;
  int keys = 1;
  String drop;

  // StreamController _streamController;
  // Stream _stream;
  List<TranslateLetter> translateletter;


  _onChanged(String value) {
    setState(() {
      charLength = value.length;
    });
  }


  @override
  void dispose() {
    translate.dispose();
    super.dispose();
  }

  void hiveOperation() async {
    await openBox();
  }

  @override
  void initState() {
    super.initState();
    hiveOperation();

    // _streamController = StreamController();
    // _stream = _streamController.stream;
  }

  void putdata() {
    setState(() {
      box.put(keys, translate.text);
    });
  }

  String getdata(keys, String drop) {
    setState(() {
      if (box.isEmpty == true) {
        drop = "Null";
      }
      else {
        drop = box.get(keys);
      }
    });
  }


  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Center(
            child: Text('Translate',
              style: TextStyle(
                  fontFamily: 'Risque',
                  color: Color.fromARGB(250, 189, 208, 156)
              ),),
          ),
          backgroundColor: Color.fromARGB(250, 64, 79, 36),
        ),
        body: Column(
            mainAxisAlignment: MainAxisAlignment.start,
            children: [
              RaisedButton(onPressed: () {},
                  child: Text('Guide')),


              Expanded(
                child: Container(child: Column(
                  children: [
                    TextField(

                      controller: translate,
                      autocorrect: true,
                      decoration: InputDecoration(
                          hintText: 'Enter Some Text Here',
                          suffixIcon: IconButton(
                            icon: Icon(Icons.arrow_drop_down,),
                            onPressed: () {

                            },
                          )),
                      onChanged: _onChanged,
                    ),
                    DropdownButton(

                      items: [

                        DropdownMenuItem<String>(
                          child: Text(

                              box.get(1)
                          ),
                          onTap: () {
                            translate.text = box.get(1);
                          },
                        ),
                        DropdownMenuItem(
                          value: box.get(2),
                          child: Text(
                            box.get(2),

                          ),
                          onTap: () {
                            translate.text = box.get(2);
                          },),
                        DropdownMenuItem(
                          value: box.get(3),
                          child: Text(
                            box.get(3),

                          ),
                          onTap: () {
                            translate.text = box.get(3);
                          },),
                        DropdownMenuItem(
                          value: box.get(4),
                          child: Text(
                            box.get(4),

                          ),
                          onTap: () {
                            translate.text = box.get(4);
                          },),
                        DropdownMenuItem(
                          value: box.get(5),
                          child: Text(
                            box.get(5),

                          ),
                          onTap: () {
                            translate.text = box.get(5);
                          },)
                      ], onChanged: (value) {
                      box.add(keys);
                    },

                    )
                  ],
                ),
                    color: Colors.green[100]),

              ),
              RaisedButton(onPressed: () {
                Translate();
              },
                  child: Text('translate')),
              Expanded(
                child: Container(
                  width: MediaQuery
                      .of(context)
                      .size
                      .width,
                  height: MediaQuery
                      .of(context)
                      .size
                      .height * 0.6,
                  color: Colors.brown[500],
                  child: Column(

                    children: [

                      Row(
                          children: [
                            Padding(
                                padding: EdgeInsets.all(8.0),
                                child:
                                Text("Length = $charLength",
                                    style: TextStyle(fontSize: 20))
                            ),
                            MaterialButton(onPressed: () {},
                                child: Icon(Icons.clear)),
                            MaterialButton(onPressed: () {},
                                child: Icon(Icons.copy))
                          ]
                      ),
                      Text(translateoutput,
                          style: TextStyle(
                              fontFamily: 'Baybayin'
                          ))
                    ],
                  ),
                ),
              )

            ]
        )
    );
  }

  void Translate() {
    // int chareven = charLength * even;
    // setState(() {
    //
    //   print(chareven);
    //
    //   if(charLength == even){
    //     translate.text.split("");
    //   }
    //
    //


// if (translate.text == null) {
//   translateoutput= "null";
// }
// else
//   translateoutput = mydata[0][translate.text];
//  print(translateoutput);


    // });
    // if (translate.text != mydata[0][translate.text]) {
    //   translate.text = translate.text;
    //
    // }
    // else{
    //   translate.text = mydata[0][translate.text];
    // }

    // Queue.from(letters);
    // print(letters);

    setState(() {
      if (translateoutput != mydata[0][translate.text]) {
        translateoutput = translate.text;
      }
      else {
        translateoutput = mydata[0][translate.text];
      }
    });
    putdata();
    print(box.get(keys));
    keys++;
    box.add(translate.text);
  }
}
4

0 回答 0