Learn Courses My Dashboard

Cannot find 'withAnimation ' in scope

Good evening. I am trying to add code to save and sort favorites in a List. Unfortunately I can’t figure out the error I get withAnimation. I believe it has to do with explicit operations but I am new to this and can’t figure it out. Thanks for any help or guidance to continue my studies.

Error: cannot find ‘withAnimation’ in scope.

import Foundation

extension MyFlightView {
    final class MyFlightViewModel: ObservableObject {
        @Published var items = [Title]()
        @Published var showingFavs = false
        @Published var savedItems: Set<Int> = [1]
        var filteredItems: [Title] {
            if showingFavs {
                return items.filter { savedItems.contains($0.ids)}
            } else {
                return items
        private var db = Database()
        init() {
            //self.savedItems = db.load()
            self.items = items
        func sortFavs() {
          withAnimation() {
        func contains(_ item: Title) -> Bool {
        func toggleFav(item: Title) {
            if contains(item) {
            } else {
            db.save(items: savedItems)
import SwiftUI

struct MyFlightView: View {
    //reference the view model
    @ObservedObject var model = TitleModel()
    @StateObject private var vm = MyFlightViewModel()
    var body: some View {
        VStack {
            Button("Toggle Favorites", action: vm.sortFavs)
    NavigationView {
            List {
                ForEach(model.titles) { item in
                        NavigationLink(destination: DetailView(detail: item),
                                       label: {
                            HStack(spacing: 20.0) {
                                    .frame (width: 50, height: 50, alignment: .center)
                                Image(systemName: vm.contains(item) ?
                                      "star.fill" : "star")
                                .onTapGesture {
                                    vm.toggleFav(item: item)

                .onMove(perform: move)
            .toolbar {
    func move(from source:IndexSet, to destination: Int) {
        (model.titles).move(fromOffsets: source, toOffset: destination)

struct MyFlightView_Previews: PreviewProvider {
    static var previews: some View {

You will have to import SwiftUI in your file where you have your extension defined.

Thank you sir!

Good morning Chris. Thanks for the help on the addition of the ‘import SwiftUI’. That fixed the error however the “Toggle Favorites” Button still does not work. My intent is to only List the identified title favorites or every title when it is toggled. I think it has something to do with the function sortFavs which calls the withAnimation. I have watched a few tutorials outside of your site. Is there anything on your site that I can study to help me out? I have attached the file. The Toggle button is in ‘MyFlightView’. Thanks for your continued support in my studies. After this issue is resolved I can move forward with finalizing the UI and content.

Enjoy your day,



Hi Shawn,

The file on Google Drive is restricted access requiring authentication. Probably best to PM me with the code if you don’t want to make it public.