Mengikuti tutorial "sekolah koding flutter - 6 merapikan class sendiri "

import 'dart:math';

import 'package:flutter/material.dart';
import 'package:english_words/english_words.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Selamat Datang',
      home: RandomWord(),
    );
  }
}

class RandomWord extends StatefulWidget {
  @override
  _RandomWordState createState() => _RandomWordState();
}

class _RandomWordState extends State<RandomWord> {
  var savedWords = [];
  String randomWord = WordPair.random().asString;

  void _changeWord() {
    setState(() {
      randomWord = WordPair.random().asString;
    });
  }

  void _saveWords() {
    savedWords.add(randomWord);
    Navigator.push(
        context,
        MaterialPageRoute(
            builder: (context) => SavedWordScreen(savedWords: savedWords)));
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(title: Text('Aplikasi Flutter ke 2')),
        body: Center(
            child:
                Column(mainAxisAlignment: MainAxisAlignment.center, children: [
          Text(randomWord, style: TextStyle(fontSize: 40)),
          IconButton(icon: Icon(Icons.favorite_border), onPressed: _saveWords)
        ])),
        floatingActionButton: FloatingActionButton(
            onPressed: _changeWord, child: Icon(Icons.play_arrow)));
  }
}

class SavedWordScreen extends StatelessWidget {
  var savedWords = [];

  SavedWordScreen({this.savedWords}); //error disini

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text('Saved Word'),
        ),
        body: Center(
            child:
                Text(savedWords.join(" - "), style: TextStyle(fontSize: 40))));
  }
}

Saya buat seperti ini tapi masih terdapat error apakah yang saya buat ada yang salah. Sudah coba untuk mancari masalah nya tapi masih belum ketemu.

Mohon bantuannya para master dan suhu.

avatar kuriyanto
@kuriyanto

5 Kontribusi 1 Poin

Dipost 2 tahun yang lalu

Tanggapan

coba dijelaskan ya masalahnya apa /errornya apa

saya kirim dengan gambar pak,

1 Jawaban:

<div><figure data-trix-attachment="{&quot;contentType&quot;:&quot;image/png&quot;,&quot;filename&quot;:&quot;Screenshot from 2022-04-22 23-56-27.png&quot;,&quot;filesize&quot;:206924,&quot;height&quot;:699,&quot;url&quot;:&quot;https://i.ibb.co/1KHM182/Screenshot-from-2022-04-22-23-56-27.png&quot;,&quot;width&quot;:1363}" data-trix-content-type="image/png" data-trix-attributes="{&quot;presentation&quot;:&quot;gallery&quot;}" class="attachment attachment--preview attachment--png"><img src="https://i.ibb.co/1KHM182/Screenshot-from-2022-04-22-23-56-27.png" width="1363" height="699"><figcaption class="attachment__caption"><span class="attachment__name">Screenshot from 2022-04-22 23-56-27.png</span></figcaption></figure>Terdapat error nya seperti berikut pak, saya coba mencari error nya sendiri tapi belum ketemu pak.<br><br>@hilmanski</div><div><br></div>

avatar kuriyanto
@kuriyanto

5 Kontribusi 1 Poin

Dipost 2 tahun yang lalu

Tanggapan

Coba kamu pasang required sebelum this.savedWords dikarenakan versi yang ini sudah null safety

coba diliat aja tutorial ini mengenai null safety https://youtu.be/W_iLrHFSG-s

Login untuk ikut Jawaban