I am an absolute beginner to Flutter. I've been trying to follow various books, youtube videos, web guides, and in almost all of them fail when I'm following them at the very first hello world type Widget.

Here is an example of a bit of code given by a guide, which throws up a long error on a red screen on the virtual device, moaning about directionality:

import 'package:flutter/material.dart';

void main() => runApp(MaterialApp(
  home: Text('Hello World'),
));

But if I change it like so, it works:

home: Text('Hello World', textDirection: TextDirection.ltr),

Firstly, do I need to set the textDirection for every text field, or can I set it globally? I'm surprised I have to set it at all - I would have thought left to right would have been the default if it wasn't specified.

Secondly, has flutter changed this recently? Every guide or tutorial or book I found was doesn't seem to have any textDirection set anywhere, has it changed or do I have an issue with my setup/config?


Solution 1: Benedikt J Schlegel

This is odd. You do not have to set textDirection usually. Infact, I never have. I believe your issue is just the missing Scaffold Widget.

Every Page should be contained in a Scaffold, it sets alot of standard values and is very important for many reasons.

 return Scaffold(
      body: Text("Hello World")

Here is a more complete. Standard starting point for a Flutter App.

main() calls runApp(MyApp)); which in turn sets MyHomePage() as it's starting point. In MyApp you can set some Appwide parameters, like Themes. Your actual UI and Code then goes into MyHomePage.

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
//This is your actual UI
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Tryout"),
      ),
      body: Center(

      ),
    );
  }
}