Repository: fyne-io/fyne
File: dialog/file.go (Go)

Code before:
		return len(f.data)
	}
	template := func() fyne.CanvasObject {
		return f.newFileItem(storage.NewFileURI("./tempfile"), true, false)
	}
	update := func(id widget.GridWrapItemID, o fyne.CanvasObject) {
		dir := f.data[id]
		parent := id == 0 && len(dir.Path()) < len(f.dir.Path())
		_, isDir := dir.(fyne.ListableURI)
		o.(*fileDialogItem).setLocation(dir, isDir || parent, parent)
	}
	choose := func(id int) {
		f.selectedID = id
		f.setSelected(f.data[id], id)
	}
	if f.view == gridView {
		grid := widget.NewGridWrap(count, template, update)
		grid.OnSelected = choose
		f.files = grid
		fyne.CurrentApp().Preferences().SetString("fileDialogViewLayout", "grid")
	} else {
		list := widget.NewList(count, template, update)
		list.OnSelected = choose
		f.files = list
		fyne.CurrentApp().Preferences().SetString("fileDialogViewLayout", "list")
	}
	if f.dir != nil {
		f.refreshDir(f.dir)
	}
	f.filesScroll.Content = container.NewPadded(f.files)
	f.filesScroll.Refresh()
}

// effectiveStartingDir calculates the directory at which the file dialog should
// open, based on the values of startingDirectory, CWD, home, and any error
// conditions which occur.
//
// Order of precedence is:
//
//   - file.startingDirectory if non-empty, os.Stat()-able, and uses the file://
//     URI scheme
//   - os.UserHomeDir()
//   - os.Getwd()
//   - "/" (should be filesystem root on all supported platforms)
func (f *FileDialog) effectiveStartingDir() fyne.ListableURI {
	if f.startingLocation != nil {
		if f.startingLocation.Scheme() == "file" {
			path := f.startingLocation.Path()

			// the starting directory is set explicitly
			if _, err := os.Stat(path); err != nil {

Review comment (suggestion):
With my suggestion to not store strings in the preferences, you can hoist it out of the if-statement.
```suggestion
	} else {
		list := widget.NewList(count, template, update)
		list.OnSelected = choose
		f.files = list
	}
	
	fyne.CurrentApp().Preferences().SetInt("fileDialogViewLayout", int(view))
```

Code after:
	f.selectedID = id

	if file == nil || file.String()[len(file.Scheme())+3:] == "" {
		// keep user input while navigating
		// in a FileSave dialog
		if !f.file.save {
			f.fileName.SetText("")
			f.open.Disable()
		}
	} else {
		f.fileName.SetText(file.Name())
		f.open.Enable()
	}
}

func (f *fileDialog) setView(view viewLayout) {
	f.view = view
	fyne.CurrentApp().Preferences().SetInt(viewLayoutKey, int(view))

	count := func() int {
		return len(f.data)
	}
	template := func() fyne.CanvasObject {
		return f.newFileItem(storage.NewFileURI("./tempfile"), true, false)
	}
	update := func(id widget.GridWrapItemID, o fyne.CanvasObject) {
		dir := f.data[id]
		parent := id == 0 && len(dir.Path()) < len(f.dir.Path())
		_, isDir := dir.(fyne.ListableURI)
		o.(*fileDialogItem).setLocation(dir, isDir || parent, parent)
	}
	choose := func(id int) {
		f.selectedID = id
		f.setSelected(f.data[id], id)
	}
	if f.view == gridView {
		grid := widget.NewGridWrap(count, template, update)
		grid.OnSelected = choose
		f.files = grid
	} else {
		list := widget.NewList(count, template, update)
		list.OnSelected = choose
		f.files = list
	}
	if f.dir != nil {
		f.refreshDir(f.dir)
	}
	f.filesScroll.Content = container.NewPadded(f.files)
	f.filesScroll.Refresh()
}