Greg
Greg

Reputation: 34798

Flutter tutorial getting "The argument for the named parameter 'child' was already specified" for two "child" within a "center"

I'm getting (in Android Studio) the following error for the following Flutter tutorial code.

Error: The argument for the named parameter 'child' was already specified.

Code Section (from: https://flutter.dev/docs/get-started/codelab)

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final wordPair = WordPair.random();
    return MaterialApp(
      title: 'Welcome to Flutter',
      home: Scaffold(
        appBar: AppBar(
          title: Text('Welcome to Flutter'),
        ),
        body: Center(
          child: Text(wordPair.asPascalCase),
          child: RandomWords()
        ),
      ),
    );
  }
}

Image snapshot for context:

enter image description here

Upvotes: 0

Views: 1647

Answers (1)

chunhunghan
chunhunghan

Reputation: 54367

You can copy paste run full code below
You can use Column wrap Text(wordPair.asPascalCase) and Expanded(child: RandomWords())
code snippet

body: Center(
          child: Column(
            children: [
              Text(wordPair.asPascalCase),
              Expanded(child: RandomWords()),
            ],
          ),
        ),

working demo

enter image description here

full code

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) {
    final wordPair = WordPair.random();
    return MaterialApp(
      title: 'Welcome to Flutter',
      home: Scaffold(
        appBar: AppBar(
          title: Text('Welcome to Flutter'),
        ),
        body: Center(
          child: Column(
            children: [
              Text(wordPair.asPascalCase),
              Expanded(child: RandomWords()),
            ],
          ),
        ),
      ),
    );
  }
}

class _RandomWordsState extends State<RandomWords> {
  final _suggestions = <WordPair>[];
  final _biggerFont = TextStyle(fontSize: 18.0);
  // #enddocregion RWS-var

  // #docregion _buildSuggestions
  Widget _buildSuggestions() {
    return ListView.builder(
        padding: EdgeInsets.all(16.0),
        itemBuilder: /*1*/ (context, i) {
          if (i.isOdd) return Divider(); /*2*/

          final index = i ~/ 2; /*3*/
          if (index >= _suggestions.length) {
            _suggestions.addAll(generateWordPairs().take(10)); /*4*/
          }
          return _buildRow(_suggestions[index]);
        });
  }
  // #enddocregion _buildSuggestions

  // #docregion _buildRow
  Widget _buildRow(WordPair pair) {
    return ListTile(
      title: Text(
        pair.asPascalCase,
        style: _biggerFont,
      ),
    );
  }
  // #enddocregion _buildRow

  // #docregion RWS-build
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Startup Name Generator'),
      ),
      body: _buildSuggestions(),
    );
  }
// #enddocregion RWS-build
// #docregion RWS-var
}
// #enddocregion RWS-var

class RandomWords extends StatefulWidget {
  @override
  State<RandomWords> createState() => _RandomWordsState();
}

Upvotes: 3

Related Questions